go - Golang int to uint8 converison,未检测到溢出
问题描述
我一直在制作“A Tour of Go”,并在 Pic 函数中发生了一些奇怪的行为。它涉及int -> uint8转换。dx和dy的值在程序执行时默认为256。这样嵌套 for 循环内的x+y就会上升到510!(255+255)
尽管如此,Golang 在将溢出的x+y转换为 uint8 时没有看到任何问题,但是当我将其更改为一些硬编码值时,比如说 uint8(321),我立即得到一个溢出错误。
有人可以向我解释这种奇怪的行为吗?
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
canvas := make([][]uint8, dy)
for y := 0; y < dy; y++ {
canvas[y] = make([]uint8, dx)
for x := 0; x < dx; x++ {
canvas[y][x] = uint8(x+y) // <- here it is
}
}
return canvas;
}
func main() {
pic.Show(Pic)
}
获得的错误:
go: finding module for package golang.org/x/tour/pic
go: downloading golang.org/x/tour v0.0.0-20200508155540-0608babe047d
go: found golang.org/x/tour/pic in golang.org/x/tour v0.0.0-20200508155540-0608babe047d
./prog.go:11:24: constant 321 overflows uint8
Go build failed.
解决方案
这是来自语言规范:
类型化常量的值必须始终可以由常量类型的值准确表示。以下常量表达式是非法的:
uint(-1) // -1 不能表示为单位
int(3.14) // 3.14 不能表示为 int
在您的情况下,x
and y
are int
,x+y
也是int
,并且uint8(x+y)
只是截断结果。但是,uint8(321)
根据语言规范无效。但是,这是有效的:
i:=321
x:=uint8(i)
推荐阅读
- ios - iOS 12 - NSLocale 问题
- amazon-web-services - 我们可以创建一个有权在 AWS 中创建其他 IAM 用户的 IAM 用户吗
- json.net - 为什么 Json.Net 在反序列化第一次时间会导致大量分配
- android - ConstraintLayout by Code - 过早设置约束
- python - 上传到云笔记本(如 colaboratory 或 azure 笔记本)时,jupyter 笔记本图像不显示
- javascript - 在这种情况下该功能的实现是什么
- twitter - 将流式推文存储在列表中以供进一步分析
- javascript - 12小时格式的一天的时刻js间隔
- javascript - 如何使用正则表达式验证我的文件输入名称?
- ms-word - 从 Intranet 打开文档时 Word 插件不可用