control

total := 0
for _, n := range []int{1, 2, 3} {
	total += add(n, 1)
}

Output value:

9

One loop keyword

Go has for There is no while and no do while for condition { } is the while form for { } is the infinite form

range

range over a slice gives index and value range over a map gives key and value in no guaranteed order Use _ for a value you do not need

switch

switch does not fall through by default fallthrough exists but is rare An expressionless switch reads cleanly for several boolean cases

if with init

if err := doWork(); err != nil {
	return err
}

The variable lives only in that if statement That scope discipline is one of the nicer parts of the language

Next

Functions in 04 functions

Posted