Archives de catégorie : Golang

Go get and Fork – script helper workaround

Why

If you write some Golang code, from my point of view, one of biggest pitfall of this language is dependency management.

At the beginning, get library directly from git repository seems a good idea, but between package rules, versions, your pull requests, it becomes a mess quickly.

Some sources on this problem:
Article: Forking Golang repositories on GitHub and managing the import path

StackOverflow : Using forked package import in Go

My workaround

Then, why not automate this procedure ?
In your .bashrc add:

function gofork() {
  if [ $# -ne 2 ] || [ -z "$1" ] || [ -z "$2" ]; then
    echo 'Usage: gofork yourFork originalModule'
    echo 'Example: golang github.com/YourName/go-contrib github.com/heirko/go-contrib'
    return
  fi
   echo "Go get fork $1 and replace $2 in GOPATH: $GOPATH"
   go get $1
   go get $2
   currentDir=$PWD
   cd $GOPATH/src/$1
   remote1=$(git config --get remote.origin.url)
   cd $GOPATH/src/$2
   remote2=$(git config --get remote.origin.url)
   cd $currentDir
   rm -rf $GOPATH/src/$2
   mv $GOPATH/src/$1 $GOPATH/src/$2
   cd $GOPATH/src/$2
   git remote add their $remote2
   echo Now in $GOPATH/src/$2 origin remote is $remote1
   echo And in $GOPATH/src/$2 their remote is $remote2
   cd $currentDir
}

export -f gofork

This command do

  • a ‘go get’ on source and fork repositories
  • extract remote urls
  • replace source code by fork
  • add source remote to your fork repository in case you want pull from source

You can call this script from command line or directly from a script like this:

gofork github.com/heralight/baloo gopkg.in/h2non/baloo.v2

Golang tutoriel GVM et go IRIS

Install Golang

In this article we will use GVM to manage multiple Golang version.

bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
. .bashrc
gvm install go1.4.3
gvm use go1.4.3
gvm install go1.6.2
gvm use go1.6.2 --default

If you have some trouble to compile go1.4.3, you can use binary version

gvm install go1.6.2 --binary

Prepare your environment

mkdir -p dev/go/my-project/{src,pkg,bin}
cd dev/go/my-project
gvm pkgset create --local
gvm pkgset use --local

create a basic Iris project

Fix Iris 3rc GOPATH bug

export GOPATH=$PWD

create it

go get -u -v github.com/iris-contrib/middleware/logger
go get -u -v github.com/kataras/iris/iris
iris create 

Go Unit Test

any files ended by « _test.go » will be consider as a test.
to execute it:

go test

in verbose mode

go test -test.v

to have teardown / setup in your unit test you can add a TestMain function to your test like

func TestMain(m *testing.M) {
        // setup test
	setup()
        // run tests	
        code := m.Run()
        // clear env
	shutdown()
	os.Exit(code)
}

Links

50 Shades of Go