Thursday, November 29, 2012

Golang: Installing Go PostgreSQL Driver

To install Go PostgreSQL driver (with CGO), you need to specify CGO_LDFLAGS and CGO_CFLAGS for your pg_config

CGO_CFLAGS="-I`pg_config --includedir`" CGO_LDFLAGS="-L`pg_config --libdir` -lpq" go get github.com/jbarham/gopgsqldriver

Golang: Create your local environemnt

Generally, you can install Go from the package installer that Go provides.

But as you need to install packages, you need root permission to install packages into the default system GOROOT path (like /usr/local or /opt/local.. etc)

You can provide your own GOPATH to avoid this kind of problem.

First, you should create a directory structure like this:

  ~/mygo
  ~/mygo/pkg
  ~/mygo/src
  ~/mygo/bin

Then, export this path (~/mygo) to your GOPATH in your .bashrc or .zshrc file:

  export GOPATH=$HOME/mygo:$GOPATH

And the bin path of Go:

  export PATH=$HOME/mygo/bin:$PATH

Done. Now you can install packages into your own local go environment. for example:

  go get https://bitbucket.org/mikespook/gearman-go

which won't require you to sudo.

If you need to install packages from local, you need to move your package source to ~/mygo/src directory, then build the package there, or Go will complain about go install: no install location for ...