The Easiest Way to Develop with Go — Introducing a Docker Based Go Tool
While trying to make a drop-dead simple example for running Go programs on IronWorker, I realized there’s just no simple way for us to point someone at an example repo with Go code and have them up and running in a few minutes. This can be done in other languages fairly easily because they don’t have special requirements like a GOPATH, a specific directory structure, and they typically have easier dependency management.
Table of Contents
Achieve Cloud Elasticity with Iron
Speak to us to find how you can achieve cloud elasticity with a serverless messaging queue and background task solution with free handheld support.
Is there a better way?
I’ve been thinking and writing a lot about Docker lately, and using it in almost everything I do (yes, I have a Docker man-crush). Mostly because at Iron.io, we run containers… lots of containers. In doing that, I’ve come to realize how insanely powerful Docker can be. It’s not just hype, trust me.
We’ve been showing people how to build and test their Go programs inside Docker containers for a while now, but it’s always been a pain. Putting your code in specific places, setting up a GOPATH, using a tool like go dep to vendor dependencies (which has its own requirements like your code must be in version control), mounting directories properly, etc. It can work, but it’s not pretty.
Well, today it just got prettier.
Introducing the Docker Go tool
I’ve created a Docker-based Go tool, `treeder/go`, that makes developing in Go uber easy. The only thing you need to have installed is Docker. Here are some of the features and benefits:
- You don’t need to install Go.
- You don’t need to install this tool. :mindblown:
- You don’t need to set up a GOPATH.
- You don’t need to put your code in a specific directory (eg: /src/github.com/user/hello) or have the Go directory structure, just put your code anywhere.
- Vendoring without import rewriting.
- Static compiling.
- Build into a Docker image (one command and you’ll end up with a runnable Docker image containing your program).
- Build remote git repos.
Iron.io Serverless Tools
Speak to us to learn how IronWorker and IronMQ are essential products for your application to become cloud elastic.
Let’s try the basics. First, copy and paste the following program into a file named app.go, in any directory.
Notice it has a dependency? Let’s vendor it:
That will put the dependencies in a /vendor directory.
Now let’s build it:
And run it:
Check http://localhost:8080/ . Boom. We ran that in the iron/base image which is a really small image that has everything needed to run your Go program.
More Cool Stuff
How about we build a runnable Docker image containing our program:
Now run your fresh new image:
Check http://localhost:8080/ again. Boom again. And better yet, the image should be tiny. Run `docker images` to check how big it is, should be about ~12MB total.
Or build a remote repo:
You’ll end up with a binary for that remote repository in the directory where you run that command.
Docker for development wins again. If you’ve developed in Go before, you should probably appreciate the simplicity here. This is still pretty experimental and doesn’t implement a lot of the toolchain yet, but from what I know, this is probably the easiest way to get started with Go. Using this, you can point someone at a repo and they can clone, build and run without any setup other than Docker.
Full documentation and source code for the project is here:https://github.com/treeder/go
I’d love your feedback! Create a Github issue or comment here.
Unlock the Cloud with Iron.io
Find out how IronWorker and IronMQ can help your application obtain the cloud with fanatical customer support, reliable performance, and competitive pricing.
Leave a Comment