golang `range` 关键字是否会破坏 uint 类型信息?

Does the golang `range` keyword botch uint type information?

考虑这个 golang 程序:

func main() {
    one := uint(1)
    ones := []uint{1, 1, 1}
    for x := range ones {
        if x != one {
            print("ERR")
        }
    }
}

当我尝试编译时出现意外错误:

$ go build foo.go 
# command-line-arguments
./foo.go:7: invalid operation: x != one (mismatched types int and uint)

为什么 go 认为 x 的类型是 int 而不是 uint

range返回的第一个值是索引,不是值。您需要的是:

func main() {
    one := uint(1)
    ones := []uint{1, 1, 1}
    for _, x := range ones {
        if x != one {
            print("ERR")
        }
    }
}