Go notes
Cross-compilation
Go has a straightforward cross-compiler. Set a $GOOS
and a $GOARCH
, and it just works. For example:
GOOS=windows GOARCH=amd64 go build
Which targets are available? There is a list of ‹the valid combinations of $GOOS
and $GOARCH
› in the doc (here).
By the way, for Rust cross-compilation, here is the doc.
For C, use zig cc
instead.
GoPL
The book is The Go Programming Language (A. A. A. Donovan & B. W. Kernighan). What I have got is the ‘Authorized Adaptation from the English Language edition’ (sold only in Mainland China): the preface is translated into Chinese, and the index is missing (unpardonable!).
Hello, World (§1.1)
If you run
go get gopl.io/ch1/helloworld
, it will fetch the source code and place it in the corresponding directory.[GoPL, p.2]
Today this is no longer the case. That command will tell you ‘go get
is no longer supported outside a module’.
Module is a new concept which first appeared in Go 1.11. The Go blog has posted a series of articles on modules.
Before the introduction of modules:
Go code is organized into packages, which are similar to libraries or modules in other languages.
[ibid.]
Now, the situation is different: a Go module contains some Go packages.
The terminology is a little confusing, since Python names them nearly the other way round! Pythonistas may have to unlearn that to learn Go modules (there is a note discussing the difference). I wish that Go modules were named libraries instead, because a module sounds smaller than a package to me (see also this article).
Command-Line Arguments (§1.2)
This is a way to avoid redundant separators:
var s, sep string
for i := 1; i < len(os.Args); i++ {
s += sep + os.Args[i]
sep = " "
}