2.1 KiB
Go
Go (also Golang) is a compiled programming language created by Rob Pike, Ken Thompson and others at Google, released in 2009. In contrast with other "modern" languages (such as Rust, etc.) Go is relatively simple, with the spec being about 130 pages.
Go generated executables are bloated (>1 MiB), since everything is statically linked. Using flags
like -ldflags "-s -w"
can help with the executable size by stripping debug symbols, but getting it under 1 MiB
is almost impossible with the default compiler. However, alternative implementations such as TinyGo compile to small
executables, mainly targeted for embedded use.
Furthermore, Go's runtime is quite heavy by having a garbage collector and a weird stack layout for supporting the builtin concurrency system, goroutines, which makes C interop slow and cumbersome compared to other languages.
Go being a Google project, depends on it financially, also it has a code of censorship.
Compilers
Due to Go having a formal spec (and being simple enough), Go has more than one compiler:
- gc: the main compiler, written in Go itself.
- gccgo: GNU Compiler Collection's Go compiler
- gollvm: compiler using LLVM as codegen backend.
- TinyGo: targeted at embedded systems.
- ...
Examples
Hello world
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
Compiling and running:
go run hello.go
Factorial
package main
import "fmt"
// Functions starting in uppercase are exported, not needed here but for
// demonstration purposes.
func Fact(n uint) uint {
res := uint(1)
for n > 0 { // no while loop in Go
res *= n
n--
}
return res
}
func main() {
// Array literal, the `...` is used to infer its size
// If you instead left the [] empty it becomes a slice (heap allocated)
ns := [...]uint{0, 4, 10, 1, 5, 8}
for _, n := range ns {
fmt.Printf("Fact(%d) = %d\n", n, Fact(n))
}
}