Skip to content

Go Data Types

Data types in Go specify the kind of values a variable can hold and the operations that can be performed on those values. Go is a statically-typed language, meaning variable types are known at compile time.

Go’s data types can be broadly categorized as follows:

CategoryDescription and Examples
Boolean Typesbool: Represents a boolean value, either true or false.
Example: var isActive bool = true
Numeric TypesRepresent numbers. Subdivided into:
- Integer Types: int8, int16, int32, int64, int (signed); uint8, uint16, uint32, uint64, uint (unsigned). byte is an alias for uint8. rune is an alias for int32 (represents a Unicode code point).
Example: var age int = 30, var temperature int8 = -5, var unicodeChar rune = 'G'
- Floating-Point Types: float32, float64 (IEEE-754 standard).
Example: var price float64 = 19.99
- Complex Types: complex64 (float32 real and imaginary parts), complex128 (float64 real and imaginary parts).
Example: var c complex128 = 3 + 4i
String Typesstring: Represents a sequence of bytes, typically UTF-8 encoded text. Strings are immutable.
Example: var name string = "Golang"
Derived/Composite TypesConstructed from basic types or other composite types:
- Pointer Types: e.g., *int (a pointer to an integer). Holds a memory address.
- Array Types: e.g., [5]int (an array of 5 integers). Fixed-size.
- Slice Types: e.g., []int (a slice of integers). Dynamically-sized view of an array.
- Structure Types (struct): e.g., type Point struct { X, Y int }. Groups fields.
- Function Types: Represents the signature of a function.
- Interface Types: Defines a set of method signatures.
- Map Types: e.g., map[string]int. Unordered key-value pairs.
- Channel Types: e.g., chan int. Used for goroutine communication.

Go provides architecture-independent integer types with fixed sizes, as well as implementation-dependent int and uint types.

TypeSizeRange (Approximate)
int88-bit-128 to 127
uint8 (byte)8-bit0 to 255
int1616-bit-32,768 to 32,767
uint1616-bit0 to 65,535
int32 (rune)32-bit-2.1 billion to 2.1 billion
uint3232-bit0 to 4.2 billion
int6464-bit-9 quintillion to 9 quintillion
uint6464-bit0 to 18 quintillion
int32 or 64 bitsDepends on system architecture
uint32 or 64 bitsDepends on system architecture

Go supports standard IEEE-754 floating-point numbers.

TypePrecisionDescription
float32Approx. 6-9 decimal digits32-bit floating-point number
float64Approx. 15-17 decimal digits64-bit floating-point number (default for literals like 3.14)

For complex number arithmetic.

TypeComponent TypeDescription
complex64float32Real and imaginary parts are float32
complex128float64Real and imaginary parts are float64
  • byte: Alias for uint8. Commonly used to represent byte data.
  • rune: Alias for int32. Represents a Unicode code point, often used when working with characters and strings.

Additionally, uintptr is an unsigned integer type large enough to store the uninterpreted bits of a pointer value. Its use is typically confined to low-level programming with the unsafe package.

Choosing the right data type is important for memory efficiency and program correctness. For example, using int8 for a value that will never exceed 100 saves memory compared to int64.