Go

From ArchWiki
Jump to: navigation, search

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: Mention delve. Perhaps in a Tools section. (Discuss in Talk: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.

Installation

Tango-edit-clear.pngThis article or section needs language, wiki syntax or style improvements. See Help:Style for reference.Tango-edit-clear.png

Reason: Recommends go over gcc-go without stating why. (Discuss in Talk:Go#)

The standard Go compiler is go, which can be installed from the go package. The go command also include various tooling such as go get, go doc, etc. An alternative is gcc-go, which is a Go frontend for the GNU Compiler Collection (GCC). In some cases gccgo may do better optimisations. When in doubt: use go.

An additional package that most Go developers will want to install is go-tools. This will provide various commonly used tools which will make working with Go easier, such as goimports, guru, gorename, etc.

Test your installation

You can 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 hello.go):

$ go build hello.go

Compilation with gccgo (same as go build -compiler=gccgo hello.go):

$ gccgo hello.go -o hello

$GOPATH

Go expects the source code to live inside $GOPATH, which is set to ~/go by default.

Tip: You can see all Go variables by running go env

Create that workspace:

$ mkdir -p ~/go/src

The ~/go/src directory is used to store the sources of the packages. When compiling Go will also create bin for executables and pkg to cache individual packages. You'll probably want to add ~/go/bin to the $PATH environment variable to run installed Go:

export PATH="$PATH:$HOME/go/bin"

Run go help gopath for more information.

Tip: $GOPATH works like $PATH and can contain multiple entries, this can be useful to split out packages downloaded with go get and your own source code; e.g. GOPATH=$HOME/go:$HOME/mygo

Enable cross compilation for other platforms

The following paragraph will outline the basics steps to enable cross-compilation support for Darwin, FreeBSD and MS Windows.

Download a copy of the source code from the official website and extracts its content to e.g. ~/downloads/go.

Build your downloaded Go with your system Go:

 $ cd ~/downloads/go/src
 $ GOROOT_BOOTSTRAP=/usr/lib/go GOOS=linux GOARCH=amd64 ./make.bash --no-clean

You can now build your system Go using the downloaded Go as bootstrap with the following command:

 $ cd /usr/lib/go/src; for os in darwin freebsd windows; do for arch in amd64 386; do sudo GOROOT_BOOTSTRAP="$HOME/downloads/go" GOOS=$os GOARCH=$arch ./make.bash --no-clean; done; done
Note: These commands will need to be run following each Go package update.

For more information, see FS#30287.

Troubleshooting

Jetbrains Go Plugin

If you are using a Jetbrains IDE and the Go plugin cannot find your Go SDK path, you might be using an incompatible package. Remove the gcc-go package and replace it with go. If your $GOPATH is set, the IDE should now be able to find your Go SDK at /usr/lib/go.

See also