Skip to content

LiL course Go Essential Training

Tools🔗

Charm CL tools

Built-ins🔗

Output🔗

1
2
3
import () {
    "fmt" // Formated output
}

Variables, Data Types, Structs🔗

Private and Public🔗

Upper case is automatically accessible outside of definition (public, exported symbol), lower case only internal (private, unexported symbol)

Structs🔗

Go struct example file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
type MyStruct struct {
    ID      string
    Val     float64
    Created time.Time
}

obj1 := MyStruct{"Derp", 42.0, time.Now()}
fmt.Printf("%#v", obj1) // verbose output

ID1 := obj1.ID // access single values with dot notation

// alternative declaration omitting values
obj2 := MyStruct{
    ID: "Derp",
    Val: 42.0
}

// declaration without any assigment, only defaults
var obj3 MyStruct

Define methods on structs

1
2
3
func (o MyStruct) Lifetime() time.Duration {
    return b.Created.Sub(time.Now().UTC())
}

Modules🔗

Time & Date🔗

Module time

1
2
3
4
5
6
7
mytime time.Time // time data type

// builtin functions
time.Now() // current time

// calculations
newtime = time.Now().Add(24 * 7 * time.Hour) // now + 7 days

HTTP requests🔗

net module
Go HTTP example file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import (
    "net/http"
)

func main () {
    // ...
    resp, err := http.Get(url) // basic GET request
    // ...
    defer resp.Body.Close() // resource management: close body

    ctype := resp.Header.Get("Content-Type") // read header
}