Dev Skills

Basic data types

Every built-in type, why there are so many integer sizes, and what to actually use.

Topic 4 of 98 · 5 min to read · 45 min to build
After this topic you can
  • Choose the right built-in type for a count, an id, a decimal and a money amount.
  • Predict the output of overflowing an int8 or a uint8.
  • Explain why 0.1 + 0.2 is not exactly 0.3 for float64 variables.

What a type is, and why there are so many

A type tells Go two things: what kind of value a variable holds, and what you are allowed to do with it. int holds whole numbers and can be added; string holds text and can be joined. The compiler checks this before your program runs, so passing a string where an int is expected never reaches production.

Go's built-in types are called basic types. There are more of them than in a language like Python because Go lets you say how big a number is, 8 bits or 64. That matters when you are talking to a database, a file format, or another machine.

The full list

TypeHoldsZero value
booltrue or falsefalse
stringimmutable bytes, usually UTF-8 text""
int, uintwhole numbers, 64-bit on 64-bit platforms0
int8 int16 int32 int64signed, exact size0
uint8 uint16 uint32 uint64unsigned, exact size0
bytealias for uint8 — one byte of data0
runealias for int32 — one Unicode code point0
float32, float64decimals0
complex64, complex128complex numbers (you will not use these)0

Which one do I actually use?

  • `int` for counting anything. It is the default, it is 64-bit on the servers and laptops you will deploy to, and using int32 "to save memory" on a loop counter saves nothing.
  • `int64` for database ids and anything crossing a wire format where the size must be fixed.
  • `float64` for decimals. float32 only when you have a specific reason, like graphics.
  • Never a float for money. 0.1 + 0.2 != 0.3 in binary floating point. Use integer cents (the smallest currency unit), or a decimal library.
  • `byte` for raw data, `rune` for characters. They are the same thing as uint8 and int32, but the alias tells the reader your intent.

Overflow wraps around, silently

overflow.go
var x int8 = 127
x++
fmt.Println(x) // -128 — it wrapped. No panic, no warning.
var u uint8 = 0
u--
fmt.Println(u) // 255
// the math package has the limits
fmt.Println(math.MaxInt64, math.MinInt64)
Converting to a smaller type truncates the same way: `int8(200)` on a variable gives -56. Go makes you write `int8(x)` so the narrowing is visible in the code.

Mixing types needs a conversion

Go will not add an int to a float64 for you, and turning a number into text needs the strconv package. Both get a whole lesson of their own: Type conversion.

Try it yourself

press Run — this executes on the Go Playground
package main
import "fmt"
func main() {
var small int8 = 127
small++
fmt.Println("int8 overflow:", small) // -128, silently
var count int = 3
var price float64 = 2.50
var name string = "tea"
var paid bool
var initial byte = 'T'
var letter rune = 'é'
fmt.Println(count, price, name, paid) // paid is false: its zero value
fmt.Println(initial, letter) // 84 233 — both are just numbers
fmt.Printf("%c %c\n", initial, letter) // T é — %c prints them as characters
fmt.Println(0.1+0.2 == 0.3) // true here, but only because constants are exact
a, b := 0.1, 0.2
fmt.Println(a+b == 0.3) // false: float64 variables are not
}
Watch the int8 wrap around, see that `byte` and `rune` are numbers underneath, and see the float trap.
The short version
  • int for counting, int64 for ids, float64 for decimals, never a float for money.
  • Overflow wraps silently. That is why every conversion between sizes is explicit.
  • byte is uint8 and rune is int32. The aliases exist to show intent.
  • Every type starts at its zero value: 0, "" or false.
Remember: Go gives you exact-size types for when it matters, and int for when it does not.
Build this before moving on
Overflow an int8 and a uint8 on purpose. Then declare one variable of each type in the table without a value, and print them all on one line to see every zero value.
Then try these in your own editor
  1. 1Print math.MaxInt64 and math.MinInt64, then add one to each.
  2. 2Add 0.1 and 0.2 as float64 and print the result. That is why money is not a float.
  3. 3Store the price of three items as whole cents in an int64, add them, and print the total as dollars and cents with / 100 and % 100.
Check yourselfTake the Getting started test