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")
}
}
}
Answer from Ainar-G on Stack OverflowHello gophers!
I encountered a weird conversion when iterating over strings.
I used "résumé" as string following a youtube video.
So what happend was, that it seems that go converted uint8 to int32 when using the range function.
And I am unsure why it did that, because u8 should be enough no ?
Also when checking what values the runes resemble I did get a different value for 'é' back ?
Maybe someone can clear this up for me, thanks in advance.
func main() {
var myString = "résumé"
var indexed = myString[0]
fmt.Printf("%v %T\n", indexed, indexed)
indexed = myString[1]
fmt.Printf("%v %T\n", indexed, indexed)}
Output:
Value: 114, Type: uint8
Value: 195, Type: uint8 // <- This value seems to be wrong !
And then using range it changed type to int32 for some reason ?
Can someone explain why that is ?
func main() {
/* ... */
// range does encode it to int32 ?
for index, value := range myString {
fmt.Printf("Index: %v, Value: %v, Type: %T\n", index, value, value)
}}
Output:
Index: 0, Value: 114, Type: int32
Index: 1, Value: 233, Type: int32
Index: 3, Value: 115, Type: int32
Index: 4, Value: 117, Type: int32
Index: 5, Value: 109, Type: int32
Index: 6, Value: 233, Type: int32
Then checking backwards what runes i get from uint8 using the int32 values i get the correct ones with 233 weirdly enough.
Any ideas why i get "wrong" value of 'e' from uint8 in the first place ?
func main() {
/* ... */
var rune_195 = string(uint8(195))
fmt.Println(rune_195)
var rune_233 = string(uint8(233))
fmt.Println(rune_233)}
Output:
Ã
é
https://groups.google.com/group/golang-nuts/msg/71c307e4d73024ce?pli=1
The germane part:
Since integer types use two's complement arithmetic, you can infer the min/max constant values for
intanduint. For example,const MaxUint = ^uint(0) const MinUint = 0 const MaxInt = int(MaxUint >> 1) const MinInt = -MaxInt - 1
As per @CarelZA's comment:
uint8 : 0 to 255
uint16 : 0 to 65535
uint32 : 0 to 4294967295
uint64 : 0 to 18446744073709551615
int8 : -128 to 127
int16 : -32768 to 32767
int32 : -2147483648 to 2147483647
int64 : -9223372036854775808 to 9223372036854775807
I would use math package for getting the maximal and minimal values for integers:
package main
import (
"fmt"
"math"
)
func main() {
// integer max
fmt.Printf("max int64 = %+v\n", math.MaxInt64)
fmt.Printf("max int32 = %+v\n", math.MaxInt32)
fmt.Printf("max int16 = %+v\n", math.MaxInt16)
// integer min
fmt.Printf("min int64 = %+v\n", math.MinInt64)
fmt.Printf("min int32 = %+v\n", math.MinInt32)
fmt.Printf("max float64 = %+v\n", math.MaxFloat64)
fmt.Printf("max float32 = %+v\n", math.MaxFloat32)
// etc you can see more int the `math`package
}
Output:
max int64 = 9223372036854775807
max int32 = 2147483647
max int16 = 32767
min int64 = -9223372036854775808
min int32 = -2147483648
max float64 = 1.7976931348623157e+308
max float32 = 3.4028234663852886e+38