Reputation: 156622
Consider this golang program:
func main() {
one := uint(1)
ones := []uint{1, 1, 1}
for x := range ones {
if x != one {
print("ERR")
}
}
}
When I try to compile I get an unexpected error:
$ go build foo.go
# command-line-arguments
./foo.go:7: invalid operation: x != one (mismatched types int and uint)
Why does go think x
has type int
instead of uint
?
Upvotes: 1
Views: 918
Reputation: 36259
The first value returned by range
is the index, not the value. What you need is:
func main() {
one := uint(1)
ones := []uint{1, 1, 1}
for _, x := range ones {
if x != one {
print("ERR")
}
}
}
Upvotes: 4