Running Gorilla/Mux in Docker

Go is a very simple and powerful language. I like Go.Go performance is amazing. I don't like that Go don't have proper Generics and Exceptions but besides that is all great. Go is great for DevOps because go generate a simple binary that is executable and this is really awesome.
Today I want to show to build a simple counter service using Go, Gorilla/Mux and Docker. Gorilla/Mux is a simple HTTP Router for Go. Using native JSON support from Go + Mux you can build some simple and yet powerful services.
We will use sync/atomic in order to make sure if we have a code that is threadsafe.  You will need to have Go and Docker installed in your machine.  We will use Go language version 1.10 and latest Gorilla/Mux. Let's get started!




The Dockerfile


Here we are using Go 1.10 docker image as the image. We are using to get in order to install gorilla/mux. Now we can build the docker image. We just need to run the command: $ docker run diegopacheco/go-counter.

Go Code

Go code is so simple. Here we create a router and add a simple route to "/" path. We will run the server on port 8080. As you can see here we are using sync/atomic in order to create a thread-safe integer. We are using the method atomic.AddUint64 in order to safe increment it.  This is done the function called Counter. Which just get the integer and increment and return.


Now we can run this docker by doing: $ docker run diegopacheco/go-counter then you can go to your browser and see the counter working :D

That's it. You can get the whole code here. As you can see go is simple and object which very few lines of code we can do a lot.

Cheers,
Diego Pacheco

Popular posts from this blog

C Unit Testing with Check

Having fun with Zig Language

HMAC in Java