Go Reference ยท Built-in Types
Go int, int8, int16, int32 and int64 types
Reference for Go's signed integer types: sizes and ranges of int, int8, int16, int32 and int64, wrap-around overflow, conversions and the math constants.
Go has five signed integer types: int8, int16, int32, int64 and plain int. The numbers say how many bits a value uses. int is the default integer type: it is 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems, but it is always a distinct type, never an alias of int32 or int64.
Signed integers use two's complement. Arithmetic that exceeds the range does not panic; it silently wraps around. Different integer types never mix in expressions without an explicit conversion.
var small int8 = 127 var big int64 = 1 << 40 n := 42 // int
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
| Type | Size | Range |
|---|---|---|
int8 |
8 bits | -128 to 127 |
int16 |
16 bits | -32,768 to 32,767 |
int32 |
32 bits | -2,147,483,648 to 2,147,483,647 |
int64 |
64 bits | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
int |
32 or 64 bits | same as int32 or int64, depending on the platform |
Zero value: 0.
Constants: math.MinInt8 ... math.MaxInt64, plus math.MinInt and math.MaxInt for the platform's int.
Errors / edge cases:
- Integer division or remainder by zero panics at run time (a constant zero divisor is a compile error).
- A constant that does not fit the target type, such as
var x int8 = 200, is a compile error. - Variable overflow wraps silently.
math.MinInt64 / -1does not panic; it yieldsMinInt64again.
Usage notes
- Use
intby default for counts, lengths, indexes and loop counters.len,capand slice indexing all useint. - Use sized types when the size matters: binary formats and protocols, large arrays where memory counts, or values that must be exactly 64 bits (IDs, timestamps), where
int64avoids surprises on 32-bit builds. - Conversions are explicit:
int64(x). Converting to a smaller type truncates; to a larger signed type it keeps the value. - Check before narrowing when the value comes from outside, for example
if n > math.MaxInt32. - Overflow is defined (wrap-around) for signed integers, unlike C. Detect it by comparing before the operation, or use
math/bitsfor add-with-carry helpers. - Parsing: use
strconv.Atoi()forint,strconv.ParseInt()for specific sizes. - Printing:
%dfor decimal,%bbinary,%ooctal,%xhex,%cas a character. - Untyped constants have arbitrary precision, so
1 << 62is fine as a constant even whereintis 32 bits, until you assign it to a variable.