66 lines
2.1 KiB
Markdown
66 lines
2.1 KiB
Markdown
# Go
|
|
Go (also Golang) is a compiled [programming language](programming_language.md) created by Rob Pike, Ken Thompson and others at [Google](google.md), released in 2009.
|
|
In contrast with other "modern" languages (such as [Rust](rust.md), 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](gc.md) and a weird stack layout for
|
|
supporting the builtin concurrency system, goroutines, which makes [C](c.md) interop slow and cumbersome compared to other
|
|
languages.
|
|
|
|
Go being a [Google](google.md) project, depends on it financially, also it has a [code of censorship](coc.md).
|
|
|
|
## 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](https://gcc.gnu.org/onlinedocs/gccgo/): [GNU Compiler Collection's](gcc.md) Go compiler
|
|
- [gollvm](https://go.googlesource.com/gollvm/): compiler using [LLVM](llvm.md) as codegen backend.
|
|
- [TinyGo](https://tinygo.org/): targeted at embedded systems.
|
|
- ...
|
|
|
|
## Examples
|
|
### Hello world
|
|
```go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
fmt.Println("Hello, world!")
|
|
}
|
|
```
|
|
|
|
Compiling and running:
|
|
```
|
|
go run hello.go
|
|
```
|
|
|
|
### Factorial
|
|
```go
|
|
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))
|
|
}
|
|
}
|
|
```
|