Go
Go is a statically-typed language with syntax loosely derived from that of C, adding garbage collected memory management, type safety, some dynamic-typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library.
Contents
Installation
There are two Go compilers nowadays, and they can be installed from official repositories:
- gc: common name for official set of compilers 8g(x86), 6g(amd64), 5g(arm), that can be installed with go
- fast compilation
- gccgo: frontend for gcc, part of its compiler collection, can be installed with gcc-go
- goroutines becomes full flow
- small size of the binary
- better optimization
Test your installation
Check that Go is installed correctly by building a simple program, as follows:
hello.go
package main import "fmt" func main() { fmt.Println("Hello, Arch!") }
Then run it with the go tool:
$ go run hello.go
Hello, Arch!
Compilation with standard gc compiler (same as go build -compiler=gc test.go
):
$ go build test.go
Compilation with gccgo (same as go build -compiler=gccgo test.go
):
$ gccgo test.go -o test
$GOPATH
Go dependencies, when used for example in import
statements, are searched for in the $GOPATH
variable, and then - in $GOROOT
(go installation directory, /usr/lib/go
by default). If you expect to use external dependencies, not only basic from $GOROOT
, you must specify workspace area in your ~/.bash_profile
(or equivalent):
export GOPATH=~/go
Create that workspace:
$ mkdir -p ~/go/{bin,src}
src
directory is used to store sources of the project, and bin
for executables.
Also you can add path to bin
directory in $PATH
environment variable to run installed programs (written on Go language) anywhere (like, for example, ls
):
export PATH="$PATH:$GOPATH/bin"
Note that we also add the bin
subdirectory to the $PATH
so we can run any executables that be required.
Run go help gopath
for more information.
Enable cross compilation for other platforms
The official package only supports Linux amd64, 386 and arm architectures. To support cross compilation for Darwin, FreeBSD and MS Windows, run the commands below.
$ cd /usr/lib/go/src; for os in darwin freebsd windows; do for arch in amd64 386; do sudo GOOS=$os GOARCH=$arch ./make.bash --no-clean; done; done
For more information, see FS#30287.