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

TypeSizeRange
intDetermined by system32-bit: same as int32
64-bit: same as int64
int88-bits-128 to 127
int1616-bits-32768 to 32767
int3232-bits-2147483648 to 2147483647
int6464-bits-9223372036854775808 to 9223372036854775807

Unsigned Integer (uint) Types in Go

TypeSizeRange
uintDetermined by system32-bit: same as uint32
64-bit: same as uint64
uint88-bits0 to 255
uint1616-bits0 to 65535
uint3232-bits0 to 4294967295
uint6464-bits0 to 18446744073709551615
uintptrDetermined by system32-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.