首页 > 解决方案 > Golang int to uint8 converison,未检测到溢出

问题描述

我一直在制作“A Tour of Go”,并在 Pic 函数中发生了一些奇怪的行为。它涉及int -> uint8转换。dxdy的值在程序执行时默认为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.

标签: gotype-conversion

解决方案


这是来自语言规范:

类型化常量的值必须始终可以由常量类型的值准确表示。以下常量表达式是非法的:

uint(-1) // -1 不能表示为单位

int(3.14) // 3.14 不能表示为 int

在您的情况下,xand yare intx+y也是int,并且uint8(x+y)只是截断结果。但是,uint8(321)根据语言规范无效。但是,这是有效的:

i:=321
x:=uint8(i)

推荐阅读