Basic Data Types in Go
An overview of Go's built-in data types - booleans, numeric types, strings, and architecture-dependent integers - with practical examples and memory fundamentals
Go programs are built from data. Every variable, every field, every expression result carries a value, and that value belongs to a specific type. The type determines what you can do with the data, how much memory it uses, and what happens when you combine it with other values.
The basic data types are the foundation for everything else: slices, maps, structs, and interfaces are built on top of them. Understanding these types, how they behave, how they interact, and where beginners get tripped up, makes the rest of the language easier to learn. Go is statically typed, so every variable has a fixed type at compile time. The compiler refuses to build a program where you try to store a string in an int variable or add a bool to a float64.
Boolean Type
A bool holds one of two values: true or false. It is the simplest data type and the backbone of all conditional logic and comparisons. Boolean values come from comparison operators and combine with the logical operators &&, ||, and !. A bool is not a number, and there is no implicit conversion between the two. Code like var flag bool = 1 does not compile, which prevents a whole class of bugs where a truth value accidentally participates in arithmetic.
Numeric Types
Go provides signed and unsigned integers in 8, 16, 32, and 64 bit sizes, floating-point numbers float32 and float64, and complex numbers complex64 and complex128. Two aliases appear constantly: byte is uint8, used for raw byte data, and rune is int32, used for Unicode code points. Reach for plain int and float64 by default. Choose a specific size only for binary protocols, file formats, or tight memory constraints. Floating-point arithmetic is an approximation, so 0.1 + 0.2 may not equal exactly 0.3, and you should never use floats for money.
String Type
A string is a read-only sequence of bytes. It can hold any byte value, and by convention it holds UTF-8 encoded text. Strings are immutable: once created, you cannot change individual bytes, so you build new strings with concatenation, fmt.Sprintf, or the strings package. The len function returns the number of bytes, not the number of characters. A single Unicode character can occupy several bytes in UTF-8, so iterating byte by byte gives a different picture than iterating rune by rune with for range.
Architecture-Dependent Types
int, uint, and uintptr are sized to the native word size of the CPU the code is compiled for. On a 64-bit machine they are 64 bits, on a 32-bit machine they are 32 bits. This makes int the natural default for counters and indices, but it also means code that assumes a 64-bit int breaks on 32-bit targets. Writing var n int = 1<<50 compiles on a 64-bit system but is a compile error on a 32-bit target, because the untyped constant 1<<50 does not fit in a 32-bit int. If your logic depends on an exact width, use int64 instead.
Common Mistakes That Compile But Misbehave
- Mixing
intanduintwithout conversion. Go requires explicit conversion, and unsigned types cannot represent negative values. - Assuming
len("hello")returns a character count. It returns bytes. For the number of runes, useutf8.RuneCountInString. - Comparing floating-point values with
==. Due to approximation,a == bmay fail for values that are mathematically equal. Compare within a small tolerance instead. - Relying on
intbeing 64 bits. Code that stores values beyond 2^31-1 must useint64if 32-bit compatibility matters.
Summary
The four groups cover every basic value a Go program holds: truth values, numbers, text, and machine-sized integers. The habits that make Go programs predictable are the same across all four: use int and float64 for everyday values, treat strings as immutable byte sequences, convert types explicitly when moving between numeric categories, and pin a size with int32 or int64 whenever a value leaves your process in binary form.
Boolean Type
Understand Go's bool type, its two possible values, default zero value, logical operators, strict type rules.
Numeric Types
An in-depth guide to Go's numeric data types covering signed and unsigned integers, floating-point numbers, complex numbers, and type aliases byte and rune
String Type
How Go represents text with the immutable string type, covering UTF-8 encoding, byte-level indexing, rune iteration.
Architecture-Dependent Types
Understand Go's architecture-dependent integer types int and uint, how their size changes across 32-bit and 64-bit systems.