A Self Contained Go Development Environment

Go(lang) is getting momentum in Indonesia during the recent years as big names are migrating to Go. Tokopedia, Go-Jek, I’m looking at you both. Not to mention many other startups build their tech-stack using Go from the get-go due to the hype.

Go is an interesting programming language, not to mention it has fun part in it. It combines the ease of dynamically typed language and the efficiency of statically typed language with support for networked and multicore computing (Go FAQ page, 2017). Moreover, we can also use some functional programming techniques in Go, fun, Right!

However, there was one thing that bothering me when I started to learn Go. It requires me to set up all projects inside its /go/src directory which is a bit inconvenient for me as I need to reorganize my directories’ structure to do so. I put all my programming projects under Codes directory, and I wanted it be the same even when coding Go program.

Then, an idea to use Docker to create a development environment for Go development popped up. I have used Docker as a development tool to build and run a JavaScript application so I thought it should be possible to create a development environment for Go.

The requirement was simple: I should not need to install anything to program with Go except for downloading the Docker image. Then, the challenges came. Firstly, the editor should be embedded in the Docker image as all go tools are inside the container it self. This is solved by using the awesome Fatih’s vim-go that provided essential tools to write, build and test Go programs. Secondly, as the development image would use vim as the editor, the vimrc should use my vimrc to avoid drop of productivity. This was resolved by creating vimrc to container’s /etc/vim/ that will be load when starting up.

Thirdly, is to make sure that the project’s files are owned by the developer’s machine user. To solve this, I leveraged the use anonymous nobody user in the container by providing -u uid:gid (user id:group id) option. By doing so, the owner of the edited files will be the same as the given uid and guid. Otherwise, the code will be owned by root because it is the default user of the container.

After experimenting (and quite long hiatus) I developed godev, a self contained Go development environment. As usual, the project is hosted in github while the resulting image can be found in docker hub.

To try the container, run the following command:

$ docker run -it -v ${PWD}:/go/src/app  -u `id -u`:`id -g` npatmaja/godev sh -l
More Reading

Hello, my name is Nauval. I like building/crafting things. I code for living. I blog in my spare time.

Have a look around and don't hesitate to contact me with any comments, suggestions or even to just say Hi!