summaryrefslogtreecommitdiff
path: root/daily/17-May-2025.md
blob: 37291102e1025371d8732205180f65a67e1f0923 (plain)
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
# Notes on go
Go compiles into native machine code.
Go handles Unicode natively thus any character can be used in a string.
Divided into packages, where a package is sort of a library, except for main which defines an executable.
Import declarations **must** follow the package declaration.
Function declarations: `func <name> (<param list>) (<result list>)`. No semicolons, unless multiple statements on 1 line.
`gofmt` mandatory. `goimports` (`golang.org/x/tools/cmd/goimports`) manages imports.
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 `_`.