Skip to content

Instantly share code, notes, and snippets.

@DoZator
Last active July 21, 2023 10:27
Show Gist options
  • Save DoZator/27dd60bf67a84720e879 to your computer and use it in GitHub Desktop.
Save DoZator/27dd60bf67a84720e879 to your computer and use it in GitHub Desktop.
Install Golang, building Golang programs

Install Go with Homebrew (OS X)

    brew install go

Create directories for GOPATH:

    mkdir ~/go
    mkdir ~/go/src

Add to .bash_profile

    export GOROOT=/usr/local/opt/go/libexec
    export GOPATH=$HOME/go
    export PATH="$GOPATH/bin:$PATH"

Install Go on Linux

Download the latest distribution:

    curl -O https://storage.googleapis.com/golang/go1.10.linux-amd64.tar.gz

Unpack it to the /usr/local

    sudo tar -C /usr/local -xzf go1.10.linux-amd64.tar.gz

For Go to work properly, you need to set the following two environment variables:

Setup a go folder

    mkdir -p ~/go; echo "export GOPATH=$HOME/go" >> ~/.bashrc

Update your path

    echo "export PATH=$PATH:$HOME/go/bin:/usr/local/go/bin" >> ~/.bashrc

Read the environment variables into current session:

    source ~/.bashrc

Cross compiling Go

    GOOS=linux GOARCH=amd64 go build hello_world.go
    GOOS=darwin GOARCH=amd64 go build hello_world.go
    GOOS=windows GOARCH=amd64 go build hello_world.go
    GOOS=windows GOARCH=386 go build hello_world.go
    GOOS=linux GOARCH=arm go build hello_world.go

List supported platforms:

    go tool dist list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment