Home »
Golang
Go Integer Data Types
Last Updated : May 18, 2025
In Go, integer data types are used to store whole numbers, and they can be either positive, negative, or only positive depending on the type.
Go has several integer types categorized by size and whether they can store negative numbers:
- Signed Integers
- Unsigned Integers
Signed Integers
Signed integer types can store both negative and positive whole numbers. Below are the signed integer types along with their value ranges and memory size:
Type |
Range |
Memory Size |
int8 |
-128 to 127 |
1 byte |
int16 |
-32,768 to 32,767 |
2 bytes |
int32 |
-2,147,483,648 to 2,147,483,647 |
4 bytes |
int64 |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
8 bytes |
int |
Platform dependent (32 or 64 bit) |
4 or 8 bytes |
Unsigned Integers
Unsigned integer types can only store positive whole numbers (including zero). Below are the unsigned integer types along with their value ranges and memory size:
Type |
Range |
Memory Size |
uint8 |
0 to 255 |
1 byte |
uint16 |
0 to 65,535 |
2 bytes |
uint32 |
0 to 4,294,967,295 |
4 bytes |
uint64 |
0 to 18,446,744,073,709,551,615 |
8 bytes |
uint |
Platform dependent (32 or 64 bit) |
4 or 8 bytes |
Declare Integer Variables
You can declare integer variables using either the var
keyword or the shorthand syntax:
var age int = 30
height := 175
Example
In this example, we declare and print different integer variables:
package main
import "fmt"
func main() {
var age int = 30
var temperature int16 = -5
var distance uint = 100
fmt.Println("Age:", age)
fmt.Println("Temperature:", temperature)
fmt.Println("Distance:", distance)
}
When executed, this program outputs:
Age: 30
Temperature: -5
Distance: 100
Default Value of Integers
When integer variables are declared without initialization, they get a default value of 0
.
Example
package main
import "fmt"
func main() {
var count int
fmt.Println("Default value of count:", count)
}
When executed, this program outputs:
Default value of count: 0
Integer Arithmetic
Go supports standard arithmetic operations with integers: +
, -
, *
, /
, and %
(modulo).
Example
package main
import "fmt"
func main() {
a := 15
b := 4
fmt.Println("Addition:", a + b)
fmt.Println("Subtraction:", a - b)
fmt.Println("Multiplication:", a * b)
fmt.Println("Division:", a / b)
fmt.Println("Modulo:", a % b)
}
When executed, this program outputs:
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3
Modulo: 3
Integer Overflow
Working with integer data types, an integer overflow occurs when a value exceeds the range allowed by its type. Go language does not raise an error but wraps around the value.
Example
package main
import "fmt"
func main() {
var smallInt int8 = 127
smallInt += 1
fmt.Println("Overflowed value:", smallInt)
}
When executed, this program outputs:
Overflowed value: -128
Conversion Between Integer Types
The values between different integer types can be converted explicitly by using the type casting. Before the variable/value, you need to specify the specific data type.
Example
This example demonstrates the conversion between the integer types (small to int16):
package main
import "fmt"
func main() {
var small int8 = 100
var large int16
large = int16(small)
fmt.Println("Converted value:", large)
}
When executed, this program outputs:
Converted value: 100
Integer Data Types Exercise
Select the correct option to complete each statement about integer data types in Go.
- The default value of an integer variable is ___.
- Which operator is used for modulo (remainder) operation?
- What will be the output of the following code?
var x int8 = 127
x += 1
fmt.Println(x)
Advertisement
Advertisement