What a type is
Every value in Go has a type. The type tells Go what kind of data the value is, which operations are allowed on it, and how much memory it needs. Go also fixes the type of a variable when you declare it: a variable that starts as a whole number can never hold text later.
That strictness lets the compiler catch many mistakes before the program runs. It also means you need to know the basic types and pick the right one for each value.
The four basic types
| Type | Holds | Example values |
|---|---|---|
int | whole numbers | 5, -12, 0 |
float64 | decimal numbers | 3.14, -0.5 |
string | text | "Hello", "" |
bool | truth values | true, false |
Go has more numeric types, such as int64, uint8 and float32, but these four cover most beginner code.
Declaring a variable with a type
With var, the type goes after the name, and then comes the value:
var age int = 31
var price float64 = 9.99
var name string = "Andy"
var isStudent bool = true
Read each line as "variable name of type T starts as value". This order, name first and type second, is the reverse of many other languages.
Checking a type
The %T verb of fmt.Printf prints the type of a value:
var myInt int = 5
var myFloat float64 = 3.14
var myString string = "Hello"
var myBool bool = true
fmt.Printf("%T %T %T %T\n", myInt, myFloat, myString, myBool)
int float64 string bool
Letting Go infer the type
You will often see the shorter form :=, which works out the type from the value. A whole number like 5 becomes an int, a number with a decimal point like 3.14 becomes a float64, quoted text becomes a string, and true or false becomes a bool. The type is just as fixed as before; you simply didn't write it. This is why x := 7 is an int but x := 7.0 is a float64, even though the numbers are equal. The lesson writes the types out so that you can see them.
Choosing a type
Pick the type that matches the meaning of the data. Counts, ages and positions in a list are int. Measurements and prices with decimals are float64. Names and messages are string. Yes/no facts, such as whether a user is a student, are bool, and it helps to name them like questions, as in isStudent.
Gotchas
- Integers and floats are different types. A variable
nholding anintand one calledmyFloatholding afloat64can't be multiplied: Go reportsmismatched types int and float64. Convert one of them, for examplefloat64(n) * myFloat. - A decimal doesn't fit into an
int.var myInt int = 3.5is rejected, because 3.5 isn't a whole number. On the other hand,var f float64 = 7is fine, andfprints as7. - Quotes matter.
"true"is a string, so it can't initialise abool. A bool value is written without quotes. - Division depends on the type. With
intvalues,7 / 2is 3, because the fractional part is dropped. Withfloat64values,7.0 / 2.0is 3.5.
Zero values
A variable declared without an initial value isn't left undefined. Go gives it the zero value of its type: 0 for int and float64, "" (the empty string) for string, and false for bool. So var count int starts at 0.
The exercise, step by step
The lesson builds four variables, one per line, each with a different type.
- An integer. Declare
myIntwith typeintand give it a whole number such as5. - A float. On the next line, declare
myFloatwith typefloat64and give it a decimal such as3.14. KeepmyInt. - A string. Declare
myStringwith typestringand give it text in double quotes, for example"Hello". - A bool. Declare
myBoolwith typebooland set it totrueorfalse.
var myInt int = 5
var myFloat float64 = 3.14
var myString string = "Hello"
var myBool bool = true
Each variable uses the same pattern: var, name, type, =, value.
Key takeaways
- Every value has a type, and a variable's type never changes.
- The four basic types are
int,float64,stringandbool. - In a
vardeclaration, the type comes after the name. - Go doesn't mix
intandfloat64in one expression without a conversion. - An uninitialised variable gets its type's zero value.