Variables & Data Types — Go

Go variables have a fixed type. var name type = value works anywhere, name := value only inside a function, and its type is guessed from the value. := makes a…

In JavaScript, a variable that held a number may hold text on the next line. In Go, that next line doesn't get past the compiler. A variable is still a name that holds a value, but every Go variable has a fixed type: a whole number, a decimal number, text, or true/false.

Look at the boxes in the display case at Bimo's Meatball Stall. Each box has a name label and a contents label: the portions box only takes whole numbers, the menu box only takes text. A number box can't be filled with text, and its contents never quietly change kind. That is what statically typed means: types are checked by the compiler before the program runs.

Two ways to make a variable: var and :=

package main

import "fmt"

var stall = "Bimo's Meatball Stall" // package level: var is required

func main() {
	var menu string = "Tendon Meatballs" // var, name, type, value
	portions := 3                        // := guesses the type: int
	price := 15.0                        // has .0, so float64
	paid := true                         // bool

	portions = 4 // change the value: just =
	fmt.Println(stall, menu, portions, price, paid)
}
$ go run stall.go
Bimo's Meatball Stall Tendon Meatballs 4 15 true

Four basic types

Type Example Holds
int 3, 15, -2 whole numbers
float64 15.0, 0.1 decimal numbers
string "Tendon Meatballs" text, always in double quotes
bool true, false true or false

Money is safest stored as whole cents in an int, because decimal numbers can carry tiny rounding differences. A value of another type is refused: var portions int = "three" fails with cannot use "three" (untyped string constant) as int value in variable declaration.

Zero values: never empty

A variable made with var and no value is filled straight away with a zero value, the zero for its type. In Go no variable holds undefined.

var portions int  // 0
var price float64 // 0
var menu string   // "" (empty text)
var paid bool     // false
fmt.Println(portions, price, menu, paid)
0 0  false

There are two spaces in the middle of that output: menu is empty text, so between 0 and false there are only the separating spaces.

Conversions are written by you

The code below multiplies portions (an int) by price (a float64). Guess before you look at the output: does 45 print, or does the compile fail?

package main

import "fmt"

func main() {
	portions := 3
	price := 15.0
	fmt.Println(portions * price)
}
$ go run stall.go
# command-line-arguments
./stall.go:8:14: invalid operation: portions * price (mismatched types int and float64)

The compile fails, nothing prints. Go never changes a type quietly, so an int times a float64 is refused. Change the type yourself by writing the type's name like a function: float64(portions) * price gives 45. The other way round, int(x) with x := 15.9 drops the fraction to give 15, it does not round.

Printing with fmt.Printf

fmt.Printf prints text with a pattern. Each verb (a format word starting with %) is replaced by the next value, in order:

menu := "Tendon Meatballs"
portions := 3
price := 15.0
fmt.Printf("%s: %d portions\n", menu, portions)
fmt.Printf("%q %v %.2f %T\n", menu, portions, price, price)
Tendon Meatballs: 3 portions
"Tendon Meatballs" 3 15.00 float64

⚠️ Lines that stick together, the wrong verb, a number of the wrong type

TL;DR: Go variables have a fixed type. var name type = value works anywhere, name := value only inside a function, and its type is guessed from the value. := makes a new variable, = changes what it holds. Four basic types: int, float64, string, bool. A variable with no value gets a zero value: 0, 0, "", false. Types never change quietly: an int times a float64 is an error, write float64(portions). fmt.Printf fills its verbs in order: %s text, %d whole number, %v anything, %q quoted text, %.2f two decimals, %T the type, and \n for a new line.

⚡ var and :=

Write:

var stall = "Bimo's Meatball Stall" // package level: var is required

func main() {
	portions := 3 // := guesses the type: int
	portions = 4  // change the value: just =
	fmt.Println(stall, portions)
}

Meaning: := only works inside a function. Outside a function, at package level, you must use var. := makes a new variable, = changes what it holds.

⚡ Zero values

Guess first: var portions int with no value, then fmt.Println(portions). What prints?

0. A variable with no value is filled straight away with its type's zero value: 0 for int, "" for string, false for bool. Go has no undefined.

Meaning: Every Go variable has a fixed type and always holds something.

⚡ You write the conversion

Guess first: portions := 3 and price := 15.0. Does portions * price give 45?

No, the compile fails with mismatched types int and float64. Write float64(portions) * price.

Meaning: Go never changes a type quietly.

⚡ Printing with a pattern

Write:

fmt.Printf("%s: %d portions\n", menu, portions)

Meaning: %s is filled with text, %d with a whole number, in order. Printf doesn't move to a new line by itself, so write \n.

What you will learn