1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
[[What are Type Assertions in Go and when to use them]]
# TODO
make a note about :
statement -> expression -> declaration, etc. Idea: draw it in some way, making it visual might help remembering.
# DIGEST INTO NOTES
Slices are dynamically sized arrays that can be accessed `s[i]` or a subset can be taken `s[i:j]`. Indexing: half-open (starts at 0). `s[i:j]` yields i through j-1. Either can be omitted.
Convention: comment describing package preceding package declaration.
Variables initialised to "zero value".7689
String concatenation via `+` char.
Supports assignment operators, e.g `+=`, `*=` etc
Short-hand variable declaration requires no type, it is inferred of the value `:=` **only allowed in a function**
For-loop structure:
```
for initialization; condition; post {
// code
}
```
Either part can be left away with different semantics.
All parts gone: loop forever.
Only condition is like a while x == true;
`range` keyword to loop over a slice or array with `for index, arg := range slice`
`_` is the blank identifier, thus above if we don' need the index we can put `_`.
---
`image/color` package features the Color interface defining a Color as just a type that has a method `RGBA() (r, g, b, a uint32)`. It has a struct `RGBA` defining this method. So for example to define green we can say: `color.RGBA{0x00, 0xFF, 0x00, 0xFF}` R G B A respectively.
Packaging doing stuff with networking are grouped under `net`, e.g `net/http`
Using `io.Copy` we can copy output from a reader to a writer. This way we can for example copy a HTTP response directly to stdout instead of buffering the entire output first.
`strings.HasPrefix` exists
To get a time delta we can use `time.Since` which returns a `time.Time`. We can just call for example `Seconds()` on that to get the elapsed seconds.
Goroutines run concurrently and communicate over channels, they are started with the `go` keyword. Sending or receiving on a channel block the goroutine.
---
Order of map iteration is unspecified but random in practice to prevent reliance on ordering.
The Scanner type from the bufio package provides an easy way to read input in lines or words.
Map created with `make` is passed by reference.
Package `io/ioutil` exposes `ReadFile` and other io utility functions and types.
Casting works by `type(var)`, e.g `i := 0; string(i)`
Referencing a multi-component package is done through the last component. E.g `ioutil` for the `io/ioutil` package.
Composite literals are the form of `type{...}` they instantiate composite types, so `[]string{"a"}` is also a composite literal.
---
Gin bool required bind fails if false, needs ptr to bool. https://github.com/gin-gonic/gin/issues/814
|