Golang Integers
In Golang, integers are whole numbers without a decimal point. Examples are numbers like 1, 2, -42, and 65535.
Integer types can be divided into signed and unsigned integers. Unsigned integers cannot have a negative sign; they can only be positive. Signed integers can be either positive or negative.
In this tutorial, we will cover: the different types of signed and unsigned integers in Go, how to initialize integer variables and work with them, and type errors when working with integers.
Integer Types in Go
There are 5 types of signed integers and 6 types of unsigned integers. These are described in the tables below:
Signed Integer (int) Types in Go
Type | Size | Range |
int | Determined by system | 32-bit: same as int32 64-bit: same as int64 |
int8 | 8-bits | -128 to 127 |
int16 | 16-bits | -32768 to 32767 |
int32 | 32-bits | -2147483648 to 2147483647 |
int64 | 64-bits | -9223372036854775808 to 9223372036854775807 |
Unsigned Integer (uint) Types in Go
Type | Size | Range |
uint | Determined by system | 32-bit: same as uint32 64-bit: same as uint64 |
uint8 | 8-bits | 0 to 255 |
uint16 | 16-bits | 0 to 65535 |
uint32 | 32-bits | 0 to 4294967295 |
uint64 | 64-bits | 0 to 18446744073709551615 |
uintptr | Determined by system | 32-bit: same as uint32 64-bit: same as uint64 |
There are three special integer types, whose size is determined by the system: int, uint, and uintptr. A 32-bit system will result in a 32-bit integer, while a 64 -bit system will result in a 64-bit integer.
Working With Integers in Go
One of the most common ways of using y is by working with integer variables. To do this, we can use the var keyword followed by the name and the integer type. The value is assigned on the right side of the ‘=’ assignment operator:
var <variablename> <type> = <value>
Note that the type can be omitted and the compiler will use a default type of ‘int’.
The working example below shows how to declare and print variables of three different integer types.
package main
import (
"fmt"
)
func main() {
var num1 = 42 // Default type is 'int'
var num2 uint32 = 1 // Declaring a uint32
var num3 int64 = -8 // Using an int64 to store a negative
fmt.Println(num1)
fmt.Println(num2)
fmt.Println(num3)
}
Output:
42 1 -8
Type Errors
The Go compiler will throw an error if we attempt to combine numbers of different types. If we tried to add two of the numbers from the above example, we would have gotten an error:
package main
import (
"fmt"
)
func main() {
var num1 = 42
var num2 uint32 = 1
fmt.Println(num1 + num2)
}
Error:
./prog.go:12:14: invalid operation: num1 + num2 (mismatched types int and uint32)
Signed vs. Unsigned Integers
The tables above give us a good idea of how both signed and unsigned integers work, and how they are different from each other. Here are the important points:
- Signed integers can have both positive or negative values, but the maximum value is only half that of unsigned integers of the same size.
- Unsigned integers can hold values double that of signed integers of the same size, but can’t hold a negative value.
- The default type is int, which will be either 32-bit or 64-bit depending on the system architecture.