Setting up Golang Tools on Mac OS X
Install Go Toolchain
Download the Mac OS X package installer, open it, and follow the prompts to install the Go tools. The package installs the Go distribution to /usr/local/go
.
Setup GOPATH Workspace
Create a new workspace directory.
mkdir ~/workspace/goapps
Inside the newly created goapps
directory, create the three folders as follows:
src
for source files whose suffix is .go, .c, .g, .s.pkg
for compiled files whose suffix is .a.bin
for executable files
If not already there, create .bash_profile
in /Users/your_username
. Note, the preceding period in the filename.
Add an entry like below so GOPATH
points to the newly created goapps
workspace.
#setup path for Golang
GOPATH=~/workspace/goapps
export GOPATH
Testing Installation
Lets create a simple hello Go application to test out the environment. Create a new hello directory under $GOPATH/src
mkdir $GOPATH/src/hello
Now create a new hello.go
file with the following source so it looks like:
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
Build hello.go with the go tool:
cd $GOPATH/src/hello
go build
The command above builds an executable named hello
in the directory alongside the source. Execute the application to see the greeting:
./hello