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 Overflow
🌐
Go Packages
pkg.go.dev › builtin
builtin package - builtin - Go Packages
byte is an alias for uint8 and is equivalent to uint8 in all ways.
🌐
Educative
educative.io › answers › what-is-type-uint8-in-golang
What is type uint8 in Golang?
type uint8 in Golang is the set of all unsigned 8-bit integers. The set ranges from 0 to 255. You should use type uint8 when you strictly want a positive integer in the range 0-255.
Discussions

Weird type conversion (uint8 to i32) using range function ?
https://go.dev/blog/strings More on reddit.com
🌐 r/golang
5
0
December 21, 2023
Int vs Int8, int 32
When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type. More on reddit.com
🌐 r/golang
38
26
December 20, 2023
uint8 and uint32 don't work the same???
I believe this is because you are relying on undefined behaviour of append. If you add fmt.Println(cap(newEntry)) after newEntry := ... you'll see that it's always 8 with type uint8, but it's 2, 2, 4, 4, 4, 4 with type uint32. append will sometimes allocate a new backing array when the backing array of the current slice is not large enough. It guesses the size of that array based on the input type. So it's guessing differently for uint8 and uint32. I think there's a subtle bug in how append is being used here. I suspect the newEntry := append(old[j], ...) is the problem because you're created newEntry with a shared backing array with old[j]. You're noticing the bug because of the types, but it's there in any time append decides to share the backing array with the previous slice (or when it decides not to share, depending on what you expect). One way to solve this would be to create old and new with make and set an initial capacity. That way you'll know the backing array is always shared. More on reddit.com
🌐 r/golang
7
5
March 7, 2023
convert uint to string
The conversion from uint8 to string turns it into a string consisting of one character, Unicode code point U+0003, the "end of text" control character. https://go.dev/play/p/valhEAgrWub More on reddit.com
🌐 r/golang
11
November 4, 2024
🌐
Ado
ado.xyz › blog › go-numerical-type-ranges
Go Numerical Type Ranges - Ado.xyz
May 9, 2020 - The range for the uint8 type is between 0 and 255.
🌐
DigitalOcean
digitalocean.com › community › tutorials › understanding-data-types-in-go
Understanding Data Types in Go | DigitalOcean
May 1, 2019 - In addition to data types having different sizes, types like integers also come in two basic types: signed and unsigned. An int8 is a signed integer, and can have a value from -128 to 127. A uint8 is an unsigned integer, and can only have a positive value of 0 to 255. The ranges are based on ...
🌐
Golang Docs
golangdocs.com › home › integers in golang
Integers in Golang - Golang Docs
December 24, 2019 - If you assign a type and then use a number larger than the types range to assign it, it will fail. Below is a program trying just that. package main import ( "fmt" ) func main() { var x uint8 fmt.Println("Throws integer overflow") x = 267 // range of uint8 is 0-255 }
🌐
Codekru
codekru.com › home › golang data types
Golang Data Types - Codekru
November 6, 2022 - Range of uint8 – The range of uint8 is 0 to 28-1
Find elsewhere
🌐
Reddit
reddit.com › r/golang › weird type conversion (uint8 to i32) using range function ?
r/golang on Reddit: Weird type conversion (uint8 to i32) using range function ?
December 21, 2023 -

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:
Ã
é

🌐
Educative
educative.io › answers › what-is-type-int8-in-golang
What is type int8 in Golang?
uint8 · uint16 · uint32 · uint64 ... the number of bytes stored. A variable of type int8 can store integers ranging from - 2 ·...
🌐
GitHub
github.com › golang › go › blob › master › src › builtin › builtin.go
go/src/builtin/builtin.go at master · golang/go
// Range: 0 through 255. type uint8 uint8 · · // uint16 is the set of all unsigned 16-bit integers. // Range: 0 through 65535. type uint16 uint16 · · // uint32 is the set of all unsigned 32-bit integers.
Author   golang
🌐
Go
go.dev › src › builtin › builtin.go
- The Go Programming Language
10 */ 11 package builtin 12 13 ... 21 false = 0 != 0 // Untyped bool. 22 ) 23 24 // uint8 is the set of all unsigned 8-bit integers. 25 // Range: 0 through 255....
🌐
Go Tutorial
golangbot.com › types
Basic Data Types in Go | golangbot.com
May 5, 2024 - Please read Golang tutorial part 3: Variables of this series to learn about variables. The following are the basic data types available in Go · bool · Numeric Types · int8, int16, int32, int64, int · uint8, uint16, uint32, uint64, uint · float32, float64 ·
🌐
Boot.dev
blog.boot.dev › golang › default-native-types-golang
Don't Go to Casting Hell - Use Default Native Types in Go | Boot.dev
May 21, 2020 - bool string int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr byte // alias for uint8 rune // alias for int32 // represents a Unicode code point float32 float64 complex64 complex128
🌐
GoLinuxCloud
golinuxcloud.com › home › golang solutions › golang string to uint8 type casting [solved]
Golang string to uint8 type casting [SOLVED] | GoLinuxCloud
December 25, 2022 - We can use strconv.Atoi, fmt.Sscan, and strconv.ParseInt functions for golang string to uint8 type casting.
🌐
ZetCode
zetcode.com › golang › builtins-uint8-type
Understanding the uint8 Type in Golang
The uint8 type represents an 8-bit unsigned integer in Go. It can store values from 0 to 255.
🌐
Rotational
rotational.io › blog › ranges-of-integer-data-types
Rotational Labs | Ranges of Integer Data Types
So without further ado, here are ... int32 = -1 << 31 MaxInt64 int64 = 1<<63 - 1 MinInt64 int64 = -1 << 63 MaxUint8 uint8 = 1<<8 - 1 MaxUint16 uint16 = 1<<16 - 1 MaxUint32 uint32 = 1<<32 - 1 MaxUint64 uint64 = 1<<64 - 1 ) // ...
🌐
Quora
quora.com › In-Go-should-I-use-uint8-if-I-know-my-value-will-be-less-than-255-Is-the-purpose-to-save-memory-reservation-for-the-system
In Go, should I use uint8 if I know my value will be less than 255? Is the purpose to save memory reservation for the system? - Quora
Use the smallest unsigned integer type only when it meaningfully serves a purpose beyond a naive “it fits under 255.” Choosing uint8 vs int (or other integer types) in Go should be guided by correctness, semantics, and performance ...
🌐
Reddit
reddit.com › r/golang › uint8 and uint32 don't work the same???
r/golang on Reddit: uint8 and uint32 don't work the same???
March 7, 2023 -

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]]
}