Lesson 3 of 38 · Fundamentals
Go Data Types and Zero Values
Go is a statically typed language: every variable has a type that is fixed when the program is compiled. Types tell Go how much memory a value needs and which operations make sense, and they let the compiler catch many mistakes before your program ever runs.
The basic types
These are the types you will use every day:
| Type | Holds | Example |
|---|---|---|
int |
whole numbers (64-bit on modern machines) | 42, -7 |
float64 |
decimal numbers | 3.14, -0.5 |
string |
text (UTF-8) | "Gopher" |
bool |
true or false |
true |
byte |
one byte, an alias for uint8 |
'A' |
rune |
one Unicode character, an alias for int32 |
'é' |
Go also has sized variants such as int8, int32, uint64 and float32, but int, float64, string and bool cover almost everything in everyday code.
var lives int = 3
var speed float64 = 2.5
var name string = "Gopher"
var ready bool = true
Zero values
In many languages an uninitialised variable holds garbage or is "undefined". Not in Go: every variable declared without a value gets its type's zero value.
var count int // 0
var price float64 // 0
var title string // "" (the empty string)
var done bool // false
fmt.Println(count, price, title == "", done) // 0 0 true false
Zero values make Go code safer and shorter. A counter starts at 0, an empty string is ready to append to, and a false flag means "not yet" — no extra initialisation needed.
Type conversions
Go never converts between types behind your back. Mixing an int and a float64 in one expression is a compile error:
total := 10
count := 4
// avg := total / 2.5 // compile error: mismatched types
avg := float64(total) / float64(count)
fmt.Println(avg) // 2.5
Write the conversion explicitly with the type name used like a function: float64(x), int(y), string(r). Converting a float64 to an int truncates toward zero: int(3.99) is 3.
Watch out for integer division: 10 / 4 is 2 because both operands are integers. Convert first if you need the fraction.
Inspecting types
The %T verb of fmt.Printf prints the type of any value, which is handy while learning:
fmt.Printf("%T %T %T\n", 42, 3.0, "hi") // int float64 string
Common mistakes
- Expecting automatic conversion:
var f float64 = myIntfails. Writefloat64(myInt). - Dividing integers:
7 / 2is3, not3.5. - Comparing with
nil: basic types can't benil; the zero value of a string is"", notnil.
Assignment
You will learn to write your own functions in the next chapter. For now, the function skeletons are given — put your code where the TODO comments are and use return to hand back the result.
Average(total int, count int) float64returnstotal / countwith the decimal part.Average(10, 4)is2.5. Ifcountis0, return0.Truncate(x float64) intconvertsxto anint, dropping the fraction.Truncate(3.99)is3andTruncate(-2.7)is-2.IsZeroScore(score int) boolreports whetherscoreequals the zero value ofint.
Keep learning
Need a quick reminder? The Go cheat sheet has every topic of Learn Go on one page.
Lesson last updated on .