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

numbers - The maximum value for an int type in Go - Stack Overflow
How does one specify the maximum value representable for an unsigned integer type? I would like to know how to initialize min in the loop below that iteratively computes min and max lengths from s... More on stackoverflow.com
🌐 stackoverflow.com
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
What is the difference between int and int64 in Go? - Stack Overflow
From what I've seen so far, this is almost correct, except that I don't think int is simply an alias - it is in fact a distinct type. golang.org/pkg/builtin/#int 2014-01-31T23:50:10.757Z+00:00 ... Indeed. Go doesn't really do type aliasing. Something like type int int32 is to be treated as ... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
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.
🌐
Rotational
rotational.io › blog › ranges-of-integer-data-types
Rotational Labs | Ranges of Integer Data Types
So without further ado, here are ... int8 = -1 << 7 MaxInt16 int16 = 1<<15 - 1 MinInt16 int16 = -1 << 15 MaxInt32 int32 = 1<<31 - 1 MinInt32 int32 = -1 << 31 MaxInt64 int64 = 1<<63 - 1 MinInt64 int64 = -1 << 63 MaxUint8 uint8 ...
🌐
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.
🌐
Golang Docs
golangdocs.com › home › integers in golang
Integers in Golang - Golang Docs
December 24, 2019 - int16 (16-bit signed integer whose range is -32768 to 32767) int32 (32-bit signed integer whose range is -2147483648 to 2147483647)
🌐
Ispycode
ispycode.com › GO › Types › Integer-Min-Max-Ranges
Integer Min Max Ranges | GOLang code
GO >> Types >> Integer-Min-Max-Ranges ... := int32(0) for i := int32(0); i<=0; i-- { min = i } fmt.Println("Min:", min) } $ go run example.go Max: 2147483647 Min: -2147483648 ·...
Find elsewhere
🌐
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.
🌐
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 ·
🌐
Google Groups
groups.google.com › g › golang-nuts › c › 7J8FY07dkW0
Range over int
Ha yes, I know I resurrected an old thread but this is high in the google results. The updated syntax in go 1.4 is still doesn't allow you to use a single integer for the range. ... -- You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
🌐
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.
🌐
W3Schools
w3schools.com › go › go_integer_data_type.php
Go Integer Data Types
This example will result in an error because 1000 is out of range for int8 (which is from -128 to 127):
🌐
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
🌐
CalliCoder
callicoder.com › golang-basic-types-operators-type-conversion
Golang Basic Types, Operators and Type Conversion | CalliCoder
February 18, 2022 - Golang has two additional integer types called byte and rune that are aliases for uint8 and int32 data types respectively -
🌐
DigitalOcean
digitalocean.com › community › tutorials › understanding-data-types-in-go
Understanding Data Types in Go | DigitalOcean
May 1, 2019 - Go has the following ... 8-bit integers (-128 to 127) int16 signed 16-bit integers (-32768 to 32767) int32 signed 32-bit integers (-2147483648 to 2147483647) int64 signed 64-bit integers (-9223372036854775808 to 9223372036854775807)...
Top answer
1 of 6
78
func ParseInt(s string, base int, bitSize int) (i int64, err error)

ParseInt always returns int64.

bitSize defines the range of values.

If the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange.

http://golang.org/pkg/strconv/#ParseInt

type int int

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.

http://golang.org/pkg/builtin/#int

So int could be bigger than 32 bit in the future or on some systems like int in C.

I guess on some systems int64 might be faster than int32 because that system only works with 64-bit integers.

Here is an example of an error when bitSize is 8:

http://play.golang.org/p/_osjMqL6Nj

package main

import (
    "fmt"
    "strconv"
)

func main() {
    i, err := strconv.ParseInt("123456", 10, 8)
    fmt.Println(i, err)
}
2 of 6
36

Package strconv

func ParseInt

func ParseInt(s string, base int, bitSize int) (i int64, err error)

ParseInt interprets a string s in the given base (2 to 36) and returns the corresponding value i. If base == 0, the base is implied by the string's prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise.

The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64.

The errors that ParseInt returns have concrete type *NumError and include err.Num = s. If s is empty or contains invalid digits, err.Err = ErrSyntax; if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange.

ParseInt always returns an int64 value. Depending on bitSize, this value will fit into int, int8, int16, int32, or int64. If the value cannot be represented by a signed integer of the size given by bitSize, then err.Err = ErrRange.

The Go Programming Language Specification

Numeric types

The value of an n-bit integer is n bits wide and represented using two's complement arithmetic.

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

There is also a set of predeclared numeric types with implementation-specific sizes:

uint     either 32 or 64 bits
int      same size as uint

int is either 32 or 64 bits, depending on the implementation. Usually it's 32 bits for 32-bit compilers and 64 bits for 64-bit compilers.

To find out the size of an int or uint, use strconv.IntSize.

Package strconv

Constants

const IntSize = intSize

IntSize is the size in bits of an int or uint value.

For example,

package main

import (
    "fmt"
    "runtime"
    "strconv"
)

func main() {
    fmt.Println(runtime.Compiler, runtime.GOARCH, runtime.GOOS)
    fmt.Println(strconv.IntSize)
}

Output:

gc amd64 linux
64
🌐
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.

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