range returns an index into a collection and possibly the value at that position:
range keyword The range keyword allows you to iterate over items of a list like an array or a map. For understanding it, you could translate the range keyword to for each index of. When used with arrays and slices, it returns the integer index of the item. (tutorial)
sample code:
package main
import "fmt"
func main() {
var xs []uint8 = []uint8{255, 254, 253}
var idx int
var ui8 uint8
for idx, ui8 = range xs {
fmt.Println(idx, ui8)
}
}
output:
0 255
1 254
2 253
P.S.:
Perhaps a look at Javascript will make Go's approach less strange:
% a = [255, 254, 253]
255,254,253
% for (x in a) { print(x, " ", a[x]); }
0 255
1 254
2 253
Answer from Ekkehard.Horner on Stack OverflowWeird type conversion (uint8 to i32) using range function ?
Int vs Int8, int 32
uint8 and uint32 don't work the same???
convert uint to string
Hello 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:
Ã
é
This is possibly a stupid question, but I come from JS and i just started learning GO. Now as you may know in JS everything is Number but in GO there are int, int8, 32, 64. So, I was wandering should i get in habit of using specific int type or just use int in 99% of cases? What’s the advantage and when do you guys use it in real life if you do?
Am I crazy or is this kind of weird? By only changing data type these programs produce different output and I can't work out why even after cracking my head on this
This produces the output I expect
func main() {
var old [][]uint32 = [][]uint32{[]uint32{0}}
var new [][]uint32
for i := 0; i < 2; i++ {
for j := range old {
for k := 0; k < 2; k++ {
newEntry := append(old[j], uint32(k))
new = append(new, newEntry)
}
}
old = new
new = [][]uint32{}
}
fmt.Println(old) // [[0 0 0] [0 0 1] [0 1 0] [0 1 1]]
}vs this one that doesn't
func main() {
var old [][]uint8 = [][]uint8{[]uint8{0}}
var new [][]uint8
for i := 0; i < 2; i++ {
for j := range old {
for k := 0; k < 2; k++ {
newEntry := append(old[j], uint8(k))
new = append(new, newEntry)
}
}
old = new
new = [][]uint8{}
}
fmt.Println(old) // [[0 0 1] [0 0 1] [0 1 1] [0 1 1]]
}