Sublime Text for Golang Build System

Sublime Text for Golang Build System

Go, also commonly referred to as golang, is a programming language developed at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. It is a statically-typed language with syntax loosely derived from that of C, adding garbage collection, type safety, some dynamic-typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library.

Official Website golang.org

Install Golang on OS X via Homebrew

$ brew install go
$ brew install hg

Setting up Golang Environment

That shows where Go was installed. We need to do the following to setup Go's environment:

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

Check Environment Variables

$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/usr/local/opt/go/bin"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.4.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.4.2/libexec/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"

$GOPATH is the workspaces of go, $GOROOT is the install path of go. We can also check the existing flags using brew info go.

Go get the Basics

$ go get golang.org/x/tools/cmd/godoc
$ go get golang.org/x/tools/cmd/vet

Install Gotour

$ go get golang.org/x/tour/gotour
$ cd $GOPATH/bin
$ ./gotour
2015/07/22 16:17:21 Serving content from /usr/local/opt/go/bin/src/code.google.com/p/go-tour
2015/07/22 16:17:21 A browser window should open. If not, please visit http://127.0.0.1:3999
2015/07/22 16:17:43 accepting connection from: 127.0.0.1:51640

Sublime Text for Golang Build System

Add alise in .bash_profile

# Go tour
alias gotour=$GOPATH/bin/gotour
GOPATH="/usr/local/opt/go/bin"
GOROOT="/usr/local/Cellar/go/1.4.2/libexec"

After save

$ source ~/.bash_profile

Sublime Text Package

Install GoSublime (A Golang plugin collection for the text editor SublimeText 2 providing code completion and other IDE-like features.), Command Shift P → Install Package → GoSublime.

Setup Build System: Tools → Build System → New Build System, config file like this

{
    "path"  : "$HOME/bin:/usr/local/bin:$PATH",
    "cmd"   : ["sh","-c","go run $file"]
}

Save file, and create a test.go file.

// test
package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World")
}

Build result output like this

Sublime Text for Golang Build System

If you got an error "MarGo: Missing required environment variables: GOPATH", check setting of Go's environment.

Sublime Text for Golang Build System
29 votes, 4.23 avg. rating (84% score)