Go Notes
The Go project includes … a cultural agenda of radical simplicity.
“The Go Programming Language”, Donovan and Kernighan.
These are my notes while learning Go. It's mostly things I might forget, or which I'm likely to have to refer to.
I don't expect this to be helpful to anyone else; if you're starting out, the tour is great.
Once you've finished the tour, you'll find that the specification is brief and easily readable - you can complete the whole thing in maybe five hours. It may be useful to know before you begin that the spec starts from individual language elements before working through the methods of bringing those elements together. The result is that the first complete program appears around 95% of the way through; but by the time you get there, you should understand every line of it.
There's also this Go Cheatsheet which you'd probably prefer.
Videos
- Rob Pike - What was wrong with the old integer 80?
- Rob Pike - Origins of Go Concurrency Style
- Rob Pike - Concurrency is not Parallelism
Recall
- Only functions named with initial capitals are exported from
packages
-
defer
pushes function call onto LIFO stack, to be called at end of current function (not enclosing block). “Defer, Panic, Recover”
Trivial syntax reminders
const
like C, but declare multiple consts in one-per-line lists (like imports)for i := 0; i < 10; i++ {}
… like C; no()
; happily with enforced{}
for i < iterations {}
… init and post optional as C - no semicolons if both omittedfor {}
… ∞ loop - still no semicolonsif x < 0 {}
- again no()
. Python meets C, also in simple multiple return valuesif v := math.Pow(x, n); v < lim {
… if has optional initswitch
like C, without fallthrough (look, nobreak
s!), cases can be non-const and any type.p := &i
(for instancevar p *int = &i
) pointers as C (*p = 42
), without pointer arithmetic- Dereferencing a field
(*p).X
is simplified top.X
(i.e. notp->X
) type Vertex struct
- otherwise like C:v := Vertex{1, 2}
,v.X = 1.1
var a [10]int
array syntax, otherwise like C …- … with slices of Python (nothing fancy like
step
, and unlike Python, altering the slice alters the original - it's referencing memory)a[3:8]
,a[:8]
,a[8:]
len(s)
for slices as Pythoncap(s)
for capacity of underlying slice (distance from start of slice to end of underlying)- Looping over range (slice/map) returns index and copy of element:
for i, v := range myslice { }
- Skip index:
for _, v := range
- Skip element:
for v := range
- Maps:
- make:
m := make(map[string]int)
- insert:
m["answer"] = 42
- retrieve:
elem, ok := m["question"]
- not present,ok
isfalse
- delete:
delete(m, key)
Types
See also types package
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8
rune // alias for int32 - "represents a Unicode code point" (char begone!)
float32 float64
complex64 complex128
⁂