learning-go

The Basics: Part II

Control statements

Implicit control through the range operator for composite types:

for i, v := range myArray {
    fmt.Println(i, v)
}

for k, v := range myMap {
    fmt.Println(k, v)
}

To use the value only, replace i or k with _, which is the blank identifier.

Switch

No break statement required. default is always evaluated last regardless of where you put it.

switch a := f.Get(); a {
    case 0, 1, 2:
        fmt.Println("ok")
    case 3, 4:
    
    default:
        fmt.Println("missing")
}

Packages

Every standalone program has a main package.

You can declare anything at package scope with the exception of using the := short variable declaration operator.

Visibility control

Every name that’s capitalized is exported.

package secrets

import ...

type internal struct {
    ...
}

func GetAll(space, name string) (map[string]string, error) {
    // This can be called through secrets.GetAll()
}

A good package should embed deep functionality behind a simple API.