Installing Go by Homebrew on Mac OS X

Install go by brew

Install golang by brew. I think that’s the easiest way to install go on OS X.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ brew install go
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/go-1.4.yosemite.bottle.ta
######################################################################## 100.0%
==> Pouring go-1.4.yosemite.bottle.tar.gz
==> Caveats
As of go 1.2, a valid GOPATH is required to use the `go get` command:
  http://golang.org/doc/code.html#GOPATH

`go vet` and `go doc` are now part of the go.tools sub repo:
  http://golang.org/doc/go1.2#go_tools_godoc

To get `go vet` and `go doc` run:
  go get golang.org/x/tools/cmd/vet
  go get golang.org/x/tools/cmd/godoc

You may wish to add the GOROOT-based install location to your PATH:
  export PATH=$PATH:/usr/local/opt/go/libexec/bin
==> Summary
?  /usr/local/Cellar/go/1.4: 4557 files, 134M

Set Path for Go

Go needs paths for his root directory. I wrote like that.

1
2
3
4
5
# .zshrc
# go
export GOROOT=/usr/local/opt/go/libexec
export GOPATH=$HOME/.go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

It is actually religious problems how people set $GOPATH and $GOROOT. Let you set them up to your belief.

Run Tiny Program

To test, run tiny program of Hello World.

1
2
~/workspace
▶ vi hello.go
1
2
3
4
5
6
7
// hello.go
package main
import "fmt"

func main() {
  fmt.Printf("Hello, world!")
}
1
2
3
~/workspace
 go run hello.go
Hello, world!%

Now it’s working well!

Next time, I will make a command tool or http server in Go.

Maybe it is not good idea to web application in Go right…?