Sunday, September 18, 2016

Using dependency injection in Go

There are three ways to handle dependency injection done in Go.

Explicit, by sending the dependency to your function / method call.

Creating a wrapper to handle injecting that dependency into the code of the of the which can then be used everywhere else in the application.

Adding a field to the structure you already have with the dependency you need.

I've used the log.Printf function especially because logging can become a dependency.
Instead of importing the "log" package then calling log.Printf every time, you should be inject that as a dependency.
This can be useful in order to allow the existence of two (or more) log levels or for swapping out logging libraries without changing the code itself every time you do it.

Sunday, June 5, 2016

How to halve the performance of NATS?

Rename the subject from "a" to "MyTopicName" :)

go get for Github Chrome extension

As I'm a Go user and I frequently get packages from Github, I've made this little extension to help me out running "go get" quicker.

It works by detecting if the repository contains any "significant" amount of Go in it (using the Github breakdown of languages) and adds a button that you can click and have in the clipboard the "go get" command for that repository. There are a few options as well to control the format of the command.

It looks like this:
It's available here: and the sources are available here: Hope you'll find it useful.

Saturday, May 28, 2016

Visualizing profiling in Go: A different way

Many of you will read these lines and know already a bit about how Go can help you out and profile your applications. If not, you can have a quick look at this article Profiling Go Programs from the Go blog. As a testimony to the language, this article predates the Go 1.0 release by almost a year yet it still works today, 4 years after the initial launch of Go. In the next years it will be able to go to school and make friends. But I digress.

So, profiling in Go is a joy and Go already gives you nice tools to visualize the information. Can we do better? For example some folks at Uber thinks so and they've built They also have a wonderful presentation about it, check it out here: Profiling and Optimizing Go . But they've built it. Could we use something that exists already?

The answer is Yes! Of course we can. Because it's Go, and it's amazing, we can use KCacheGrind (and it's cousins) to visualize callgrind profiles, not to mention get a nice visualization while at it.

To give you a quick example:
  • first profile your code and save the profile as profile
  • then run go tool pprof -callgrind -output=profile.grind binaryName profile
  • the launch KCacheGrind and open profile.grind

And to give an example of how this looks like for an application, here are a few screenshots. Thank you to Olivier Gagnon for offering his code for this post.

The code below assumes Linux (Ubuntu specifically but you should be able to get it running on other platforms as well):

// First lets install the dependencies
sudo apt-get install libxrandr-dev libxcursor-dev libxinerama-dev libxi-dev

// Fetch the source code
go get

cd $GOPATH/src/

// In the import () section add:

// Right after main() { opens, add:
    f, err := os.Create("./profile")
    if err != nil {
    defer pprof.StopCPUProfile()

// Build the application
go build -o engine main_dev.go

// And run it

// Wait at least a few seconds and then press Esc to quit it

// Get the profile in callgrind format
go tool pprof -callgrind -output=profile.grind engine profile

// Finally run KCacheGrind
kcachegrind profile.grind

Note, at the time of writing the code was in flux so I had to change this:

@@ -160,8 +169,8 @@ func main() {

gravity := glm.Vec3{0, -5, 0}
gravFG := tornago.NewGravityForceGenerator(&gravity)
- w.AddForceGenerator(boxBody, &gravFG)
- w.AddForceGenerator(boxBody2, &gravFG)
+ w.AddForceGenerator(boxBody, gravFG)
+ w.AddForceGenerator(boxBody2, gravFG)

_ = str

Thank you.

Sunday, December 13, 2015

My GOPATH dir only gets bigger and bigger

I've just ran cloc on my $GOPATH and I'm a bit surprised.
Turns out that the Go plugin for IntelliJ IDEA can really process it's way into almost 35k Go files with about 8.8 million lines of code....