technology from back to front

Using goinstall for your own local code

I am working towards finishing a small project in go and my thoughts have turned to how I will package it up and release the code. The latest release of go has made some changes to goinstall so that code installed by you as a user can be kept separate from the base installation of go.

The first step in using goinstall is to create a directory structure for your local source, packages and binaries. This can be done like this

<pre>
% cd ~/work
% mkdir -p gocode/src gocode/pkg gocode/bin
</pre>

You can then create a GOPATH variable:

<pre>
export GOPATH=~/work/gocode
</pre>

Now any go projects can be placed in the

$GOPATH/src

directory and installed locally, you don’t even need to write makefiles.

For example:

<pre>
% cd ~/work/gocode/src
% mkdir -p experiment/plutonium
</pre>

Now create a file

experiment.go

in the directory:

<pre>
package plutonium

import (
  "fmt"
)

func ExperimentOne(s string) string {
  return fmt.Sprintf("! DANGER ! %s ! DANGER !", s)
}
</pre>

Now this code can be installed locally making it available to your other go projects:

<pre>
% cd ~/work/gocode/src/experiment/plutonium
% goinstall .
% ls -l ~/work/gocode/pkg/linux_386/experiment
-rw-r--r-- 1 tim tim 9004 Jun 30 13:29 plutonium.a
</pre>

You can also create executable programs:

<pre>
% cd ~/work/gocode/src
% mkdir -p experiment/bang
% cd experiment/bang
</pre>

Create a file called

main.go

:

<pre>
package main

import (
  "experiment/plutonium"
  "log"
)

func main() {
  log.Print("Running experiment", plutonium.ExperimentOne("Highly dangerous"))
  log.Print("Careful now")
}
</pre>

Install and run the program:

<pre>
% cd ~/work/gocode/src/experiment/bang
% goinstall .
% cd ~/work/gocode/bin
% ./bang
2011/06/30 13:38:45 Running experiment! DANGER ! Highly dangerous ! DANGER !
2011/06/30 13:38:45 Careful now
</pre>

So no Makefiles are needed! I would still create Makefiles because you will need them if you want to use gotest to run your unit tests.

by
tim
on
30/06/11
 
 
2000-13 LShift Ltd, 1st Floor Office, Hoxton Point, 6 Rufus Street, London, N1 6PE, UK +44 (0)20 7729 7060