Variables

Declare and initialize


var a int = 10

Inferred type


var b = 10
fmt.Println(reflect.TypeOf(b))
int

Initialized to 0


var c int
fmt.Println(c)
0

Multi initializer


var d, e int = 10, 20

Inferred multi


var f, g = 10, "wow"
fmt.Printf("%v\n%v",reflect.TypeOf(f), reflect.TypeOf(g))
int
string

Multiline initializer


var (
	h    int
	i        = 20
	j    int = 30
	k, l     = 40, "hello"
	m, n string
)
fmt.Println(h, i, j, k, l, m, n)
0 20 30 40 hello

Shorthand initializer


o := 10
p, q := 30, "hello"
fmt.Println(o, p, q)
10 30 hello

Const declaration


// Like C this swaps out the consts with literals during compile time
const (
	idKey   string = "id"
	nameKey        = "name"
)
fmt.Println(idKey, nameKey)
id name