Go Notes
Learning Go By Jon Bodner
var vs :=
Both of them are used to declare the variables, but they are slight different from each other.
varcan be used at package level declaration, but:=can only be used inside the function:=can be use to redeclare the value. For example,a, b := "a", "b"even ifaorbhas been declared before. However, it is not allowed when usingvar
array vs slice
- Go considers size of the
arrayto be part of the type of the array which means[3]intis different from[4]int. And type must be resolved at compile time not runtime time. Normally array is not recommended to use unless you know the exact size of the array at the beginning. for example in some cryptographic algorithm. slicehas more flexibily to increase the capacity of the array.- the length and capacity are two different concepts in
slice. When the length exceeds capacity, Go will double the capacity and copy the slice to a new memory address. GC handles the original one. - memory sharing properties in
sliceandarray - use
copyto create a slice that independent of original
map vs set
- Go doesn’t include
set, usemapto simulate the behaviour of a set
shadowing variables
- if variable redeclare inside a statement with
varor:=, the value remains the same as the one declared before the statement after the statement is destoryed - install
shadow(go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest) to detect shadow variables