Using Go modules

Go logo When Go was first announce I took a look, and I took another look when Go Programming Language was publised. I've started on Go multiple times and write a few minor programs. The syntax and simplicity has been very appealing to me, but I never had the time to properly learn Go. Now I'm trying again, and since the last try modules have show up.

You can still use Go without thinking about modules, mostly. One of the project I'm trinkering with uses HashiCorps Vault and here "go get" failed me. So I had to use the new mod commands to get started.

To get started, just create a new project directory as normal, and tell Go to create your go.mod file.

$ mkdir -p ~/gocode/src/dugg.dk/project
$ cd ~/gocode/src/dugg.dk/project
$ go mod init
go: creating new go.mod: module dugg.dk/project
go: to add module requirements and sums:
	go mod tidy

Now just start coding like normal, and once ready to compile run "go mod tidy" to pull the required modules.

$ go mod tidy
go: finding module for package github.com/hashicorp/vault/api
go: found github.com/hashicorp/vault/api in github.com/hashicorp/vault/api v1.1.0

If you previously written Python and used virtualenvs and pip, the easist way to think of Go modules, from the prespective of using it, is to view the go.mod file as a requirement.txt file. The file will tell the go commandline tool which package/module to pull and in which versions, effectively working much like "pip install -r requirements.txt".

It might not be all that fancy, but it takes the things I like about pip and applies them to Go.