From Go 1.22 (expected release February 2024), you will be able to write:

for i := range 10 {
    fmt.Println(i+1)
}

(ranging over an integer in Go iterates from 0 to one less than that integer).

For versions of Go before 1.22, the idiomatic approach is to write a for loop like this.

for i := 1; i <= 10; i++ {
    fmt.Println(i)
}
Answer from Paul Hankin on Stack Overflow
Discussions

For range with ints - Technical Discussion - Go Forum
chatgpt is saying that for range with integer works different, like it is converted to an iterable type first, (to string) and iterates over rune ‘5’ only once, but when I run it in playground, it is working like I would have written: for i := 0; i More on forum.golangbridge.org
🌐 forum.golangbridge.org
0
November 27, 2024
Behavior of converting uint32 --> int on 32-bit vs 64-bit systems
https://go.dev/play/p/lSJ2sDH2s_O would be equivalent to what you're asking about, int isn't any different than the more explicit int32 or int64 types. Any conversion from a max uint value to the equivalent int type will yield the same result, -1. I read the spec and the conversion section doesn't mention anything specific about converting from unsigned to signed integers in an overflow situation unfortunately, but I think evaluating to -1 is pretty typical. https://go.dev/ref/spec#Conversions More on reddit.com
🌐 r/golang
11
4
January 3, 2024
Very noob question, but I am just wrapping my head about different types of numbers... if I set a variable to be int... how could I find out what int it is? (int32, int64 etc?)
Hey not a dumb question at all. An int is a third, seperate type. But of course it does have a length- usually 64 bit but could be 32. Rule of thumb: use int only for for loops and such, and use sized ints for manipulating data. More on reddit.com
🌐 r/golang
12
17
September 24, 2023
iterating over an integer?
what are the possible uses of this construct? Analysis of the public Go code corpus has revealed that it's by far the most common use case of the 3-clause for loop. More on reddit.com
🌐 r/golang
8
6
December 27, 2023
🌐
Golang Docs
golangdocs.com › home › integers in golang
Integers in Golang - Golang Docs
December 24, 2019 - package main import ( "fmt" ) func main() { var x int32 var y uint32 // range 0 to 4294967295 var z uint8 // range 0 to 255 fmt.Println("Type Conversion") x = 26700 y = uint32(x) // data preserved because number is inside range z = uint8(x) // data loss due to out of range conversion fmt.Println(y, ...
🌐
Educative
educative.io › answers › what-is-type-int32-in-golang
What is type int32 in Golang?
A variable of type int32 can store integers ranging from -2147483648 to 2147483647.
🌐
Google Groups
groups.google.com › g › golang-nuts › c › 7J8FY07dkW0
Range over int
Don't be too much "pissed off" by people asking for those pythonic "features" or ... generics :D, golang ist awesome. One just have to realize it, which is difficult. Anyways, have a nice day everybody. ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... Having introduced this convenience range-syntax where you can specify the upper bound; wouldn't it by extension make sense to also allow the programmer to specify a lower bound à la "x := range a, b".
🌐
Go Forum
forum.golangbridge.org › technical discussion
For range with ints - Technical Discussion - Go Forum
November 27, 2024 - chatgpt is saying that for range with integer works different, like it is converted to an iterable type first, (to string) and iterates over rune ‘5’ only once, but when I run it in playground, it is working like I would have written: for i := 0; i
Find elsewhere
🌐
Reddit
reddit.com › r/golang › behavior of converting uint32 --> int on 32-bit vs 64-bit systems
r/golang on Reddit: Behavior of converting uint32 --> int on 32-bit vs 64-bit systems
January 3, 2024 -

From what I understand, the size of the int type in Go is platform dependent.

From the docs (tour):

The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.

So, on a 32-bit system, if I did something like this:

var myUint32 uint32 = 4294967295 // max positive value for uint32

myInt := int(myUint32)

what would happen to myInt on a 32-bit system? I can't seem to find docs for this. Would the bitwise value remain the same in memory (and using a bit as a sign bit)? Or would there be truncation of some sort? I am unsure. I also don't have a quick way to test this out.

backstory, I have code in review that was suggested to be written like this:

func myFunc (a, b uint32) int {
    return int(a) - int(b)
}

I have a really bad feeling about this, especially since our code planned to be open source. There is no guarantee what architecture this will run on. But I need solid evidence argue we shouldn't do this.

🌐
Reddit
reddit.com › r/golang › iterating over an integer?
r/golang on Reddit: iterating over an integer?
December 27, 2023 -

so there's an upcoming feature where you can do for x := range n where n is an integer value. is that an attempt at avoiding a range syntax like Swift, Rust, and others have (m..n or m..=n or [m..n] or m...n) while at the same time having some thing that may in some sense resemble it? what are the possible uses of this construct? what is the rationale behind adding this to the language?

EDIT: what I find weird is that int is a scalar type in Golang, as I understand it, so how can you iterate over it. I mean semantically you cannot. but this is just simple syntactic sugar. I now get that this is a shorthand for one particular and popular case of C-derived for loop scheme where you routinely type out for i := 0; i < n; i++. so you can now just say for i := range n instead. no biggie. it's a very small thing to me.

if this saves someone a search, cool.

🌐
Ado
ado.xyz › blog › go-numerical-type-ranges
Go Numerical Type Ranges - Ado.xyz
May 9, 2020 - Thus, the range for the int16 type in Go is between -32,768 and 32,767. The int32 type represents all 32-bit signed integers.
🌐
W3Schools
w3schools.com › go › go_integer_data_type.php
Go Integer Data Types
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.
🌐
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:
Ã
é

🌐
GitHub
github.com › golang › go › issues › 61405
spec: add range over int, range over func · Issue #61405 · golang/go
July 17, 2023 - Following discussion on #56413, I propose to add two new types that a for-range statement can range over: integers and functions. In the spec, the table that begins the section would have a few more rows added: Range expression 1st value...
Author   rsc
🌐
Medium
medium.com › @LukePetersonAU › understanding-integer-types-in-go-a55453f5ae00
Understanding integer types in Go | by Luke Peterson | Medium
April 8, 2024 - int16 — Represents a 16-bit signed integer. Range: -32,768 to 32,767 · int32 — Represents a 32-bit signed integer.
🌐
The Go Programming Language
tip.golang.org › src › internal › types › testdata › spec › range_int.go
ERROR "cannot range over 1.0 (untyped float constant 1)"
4 5 // This is a subset of the tests in range.go for range over integers, 6 // with extra tests, and without the need for -goexperiment=range. 7 8 package p 9 10 // test framework assumes 64-bit int/uint sizes by default 11 const ( 12 maxInt = 1<<63 - 1 13 maxUint = 1<<64 - 1 14 ) 15 16 type MyInt int32 17 18 func _() { 19 for range -1 { 20 } 21 for range 0 { 22 } 23 for range 1 { 24 } 25 for range uint8(1) { 26 } 27 for range int64(1) { 28 } 29 for range MyInt(1) { 30 } 31 for range 'x' { 32 } 33 for range 1.0 /* ERROR "cannot range over 1.0 (untyped float constant 1)" */ { 34 } 35 36 var i i
🌐
Reddit
reddit.com › r/golang › using int32 for efficiency or simply stick to int64?
r/golang on Reddit: Using int32 for efficiency or simply stick to int64?
January 30, 2022 -

I have a field in my data which is small enough to simply use an int32. However, from what I can tell, int32 is generally used when representing a code point or something quite specific, rather than using it simply to save 32 bits of storage. Also, some of the stdlib packages (like strconv) deal exclusively with int64 rather than int32. This has meant changes to my code I didn't expect purely to handle int32 things.

What is your opinion (and perhaps the general consensus) on using int32 purely for efficiency purposes? Is it just worth using an int64 for simplicity?

🌐
Go Packages
pkg.go.dev › builtin
builtin package - builtin - Go Packages
int is a signed integer type that is at least 32 bits in size. It is a distinct type, however, and not an alias for, say, int32. ... int64 is the set of all signed 64-bit integers. Range: -9223372036854775808 through 9223372036854775807.