Skip to content
Sign inGet all access
Chapter 2·18 minFree

Types & Variables

Static and inferred, zero values, and no implicit conversion anywhere.

var

var name type = value declares a variable. The type comes after the name:

main.go
package main

import "fmt"

func main() {
	var count int = 10

	fmt.Println(count)
}
Output10

Give it a value and the type is inferred; give it a type and you get the zero value. You rarely write all three parts:

main.go
package main

import "fmt"

func main() {
	var a = 10
	var b int

	fmt.Println(a)
	fmt.Println(b)
}
Output100

Short declaration

Inside a function, := declares and assigns in one step:

main.go
package main

import "fmt"

func main() {
	count := 10
	name := "ada"

	fmt.Println(count)
	fmt.Println(name)
}
Output10ada

It declares, so a second := on the same name is an error — use = to reassign:

main.go
package main

import "fmt"

func main() {
	count := 10
	count = 20

	fmt.Println(count)
}
Output20

const

A const is fixed at compile time. An untyped one takes the type it is used with, so Pi * 10 works where a float64 variable times an int would not:

main.go
package main

import "fmt"

const Pi = 3.14159

func main() {
	fmt.Println(Pi)
	fmt.Println(Pi * 10)
}
Output3.1415931.4159

Zero values

Every type has a zero value, and a declared variable always has one:

main.go
package main

import "fmt"

func main() {
	var s string
	var i int
	var f float64
	var b bool
	var p *int

	fmt.Printf("%q\n", s)
	fmt.Println(i)
	fmt.Println(f)
	fmt.Println(b)
	fmt.Println(p)
}
Output""00false<nil>

The numeric types

int is the one you reach for. The sized types exist for when the width matters:

Type Is
int, uint 64-bit on any modern machine
int8 int16 int32 int64 exactly that many bits
float64 the default float; float32 when you must
byte an alias for uint8
rune an alias for int32, holding one Unicode code point

Integer division truncates, as it does in C and Java:

main.go
package main

import "fmt"

func main() {
	fmt.Println(7 / 2)
	fmt.Println(7.0 / 2.0)
	fmt.Println(7 % 2)
}
Output33.51

Operators

Arithmetic, and the assignment shorthands:

main.go
package main

import "fmt"

func main() {
	a, b := 7, 2

	fmt.Println(a + b)
	fmt.Println(a - b)
	fmt.Println(a * b)
	fmt.Println(a / b)
	fmt.Println(a % b)
}
Output951431
main.go
package main

import "fmt"

func main() {
	n := 5

	n += 3
	n *= 2
	n++

	fmt.Println(n)
}
Output17

Comparison returns a bool, and the logical operators short-circuit:

main.go
package main

import "fmt"

func main() {
	a, b := 7, 2

	fmt.Println(a == b)
	fmt.Println(a > b)
	fmt.Println(a == b && a > b)
	fmt.Println(a == b || a > b)
}
Outputfalsetruefalsetrue

The full set:

Operators For
+ - * / % arithmetic; + also concatenates strings
+= -= *= /= %= assignment shorthand
++ -- statements only
== != < <= > >= comparison, returning bool
&& || ! logical, short-circuiting
& | ^ << >> bitwise and shifts
& * address-of and dereference (chapter 7)

No implicit conversion

Go will not mix numeric types, even widening ones:

main.go
package main

import "fmt"

func main() {
	var i int = 10
	var f float64 = 2.5

	fmt.Println(i * f)
}
Output./main.go:9:14: invalid operation: i * f (mismatched types int and float64)

Converting

T(v) converts explicitly:

main.go
package main

import "fmt"

func main() {
	var f float64 = 3.9
	var i int = int(f)

	var small int8 = 25
	var big int64 = int64(small)

	fmt.Println(i)
	fmt.Println(big)
}
Output325

A conversion that does not fit wraps around, silently:

main.go
package main

import "fmt"

func main() {
	var big int64 = 300

	fmt.Println(int8(big))
}
Output44

Text is different — strconv, not a conversion. Atoi returns two values, the number and an error, so n, _ := discards the second:

main.go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	n, _ := strconv.Atoi("42")
	fmt.Println(n + 1)

	s := strconv.Itoa(42)
	fmt.Println(s + "!")
}
Output4342!

Floats have their own pair, and ParseFloat takes the bit size:

main.go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	f, _ := strconv.ParseFloat("3.14", 64)
	fmt.Println(f * 2)

	s := strconv.FormatFloat(3.14159, 'f', 2, 64)
	fmt.Println(s)
}
Output6.283.14

Both return an error when the text does not parse:

main.go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	_, err := strconv.Atoi("abc")

	fmt.Println(err)
}
Outputstrconv.Atoi: parsing "abc": invalid syntax

Strings and bools

A string is immutable, and len counts bytes:

main.go
package main

import "fmt"

func main() {
	fmt.Println(len("hello"))
	fmt.Println(len("héllo"))
}
Output56

There is no truthiness. A condition must be an actual bool:

main.go
package main

import "fmt"

func main() {
	var count int = 1

	if count {
		fmt.Println("yes")
	}
}
Output./main.go:8:5: non-boolean condition in if statement

Your own types

type makes a genuinely new type, not an alias:

main.go
package main

import "fmt"

type UserID int

func main() {
	var id UserID = 7

	fmt.Println(id)
}
Output7

The compiler then keeps it apart from the type it was built on:

main.go
package main

import "fmt"

type UserID int

func main() {
	var id UserID = 7
	var n int = 3

	fmt.Println(id + n)
}
Output./main.go:11:14: invalid operation: id + n (mismatched types UserID and int)