Basic types
In the lesson on variables, a variable was a labeled box. Every such box has a shape. You won't fit a book into an egg carton or a saucepan into a glasses case. In Go, the shape of the box is its type.
The two variables below print the same, even though they hold values of different types:
package main
import "fmt"
func main() {
a := 3
b := 3.0
fmt.Println(a, b) // 3 3
fmt.Printf("%T %T\n", a, b) // int float64
}A type defines which values a variable holds and which operations are allowed on it. Let's go through the program line by line:
a := 3:agets the typeintbecause3is a whole number.b := 3.0:bgets the typefloat64because of the dot in3.0.fmt.Println(a, b)prints3 3: the fractional3.0is printed as3.fmt.Printf("%T %T\n", a, b)prints the types,int float64, and only here does the difference show.
From the outside the boxes look alike, but their shapes differ.
Four basic types
| Type | What it holds | Example |
|---|---|---|
int | a whole number | 42, -7 |
float64 | a fractional number | 3.14, 2.0 |
string | text | "Go" |
bool | true or false | true, false |
Let's look at each type on its own.
int: whole numbers
int holds a whole number with no fractional part: 42, -7, 0. The compiler makes a number without a dot an int, so a := 3 is a whole-number variable.
An int holds what you count piece by piece: a quantity, a number in a list, a score. If you need a fractional variable from a whole number, you write the type explicitly: var price float64 = 3.
float64: fractional numbers
float64 holds a number with a fractional part: 3.14, 2.0. A dot makes a value fractional, even when a zero follows it: 3.0 is a float64.
A float64 holds what you measure: a price, a weight, a temperature. You have already seen its trap: fmt.Println prints 3.0 as 3, so the output does not reveal the type. That is when %T helps.
string: text
string holds text, and it is written in double quotes: "Go". Strings are joined with the same + operator as numbers: "Go" + "pher" gives "Gopher".
The type decides what an operator does. For numbers + computes a sum, and for strings it glues them together.
You will also see the type rune: an integer that Go uses for one character of a string. It is written in single quotes, and the common mistake at the end of the lesson is about it.
bool: true or false
A bool has only two values: true and false. It holds a yes-or-no answer: whether an order is ready, whether the sound is on.
In Go, a bool is not a number. The line var ok bool = 1 does not compile: cannot use 1 (untyped int constant) as bool value in variable declaration.
The type is chosen once
A variable's type is chosen once, at the declaration, and does not change afterward. After a := 3, the variable a holds only whole numbers.
The assignment a = 2.5 does not compile: cannot use 2.5 (untyped float constant) as int value in assignment (truncated). The word truncated means the fractional part would be lost, and the compiler does not allow that.
Here the box picture stops working. You can try to cram something foreign into a real box, and sometimes it even fits. In Go that never works: the compiler checks the shape of every box before the program runs.
Formatted output
fmt.Printf prints text by a template—a format string. Each % sign followed by a letter in the template is replaced by the next argument. Here are the parts of a template from this lesson:
%vprints a value the same wayfmt.Printlndoes;%Tprints the value's type;\nends the line: unlikePrintln,Printfdoes not do that by itself.
%T helps when it is unclear which type the compiler took from a value.
name := "Go"
ready := true
fmt.Printf("%v %v\n", name, ready) // Go true
fmt.Printf("%T %T\n", name, ready) // string boolLet's go through the example line by line:
name := "Go"is a string, because the value is in double quotes.ready := trueis a value of typebool.- The first
Printfputs the values ofnameandreadyin place of the two%vsigns and printsGo true. - The second
Printfputs in their types and printsstring bool.
Each line of output ends with the line break from \n.
Common mistake
A string is written in single quotes, as in some other languages:
s := 'Go' // ✗
fmt.Println(s)more than one character in rune literalIn Go, single quotes hold one character—a value of type rune, not a string. Text goes in double quotes: s := "Go".
Key points
- A type defines which values a variable holds and which operations are allowed on it; it does not change after the declaration.
- The basic types are
int,float64,stringandbool;3gives anint, and3.0gives afloat64. fmt.Printfprints by a template:%vfor the value,%Tfor the type, and\nends the line.- Strings go in double quotes, while single quotes hold one character of type
rune.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.