Searching for a string in a large array

Sometimes you just need to search for a string in a very large list (let’s say 1 million strings or more) as you know the brute force search will yield a Big O Notation of O(n) which as the larger the dataset grows the slower it gets. Instead we can use a map[string]interface{} to reduce that time. Let’s create a simple test to compare the brute force search with the map solution. Read On →

Simple Go dependency injection (Part 2)

Simple Go dependency injection (Part 2) Welcome to part 2 of 2 in this brief series dedicated to Go dependency injection. In Simple Go Dependency Injection Part 1 we saw how to replace 1 method with a variable so that we could at test time ‘inject’ a mock function to replace the real one and therefore return behavior required for the unit test you were coding. That method is simple works well but what if the thing you are trying to mock is a struct with attached methods, it now becomes much more complicated to try to turn all methods into variables and then use those as you can imagine this could quickly turn into a nightmare. Read On →

Simple Go dependency injection (Part 1)

Simple Go dependency injection (Part 1) So you wrote a cool application that depends on some 3rd party code that accesses a DB and you want to make sure your code works a you want but that damn dependency makes it so that unit tests are not possible…. Think again! Below is simple app that calls fmt.Printf to print our greeting on the screen. In this simple example we want to unit test Greet to make sure the empty name produces the Hello World! Read On →

Testing stdout in golang

Intro The other day I stumbled on the Example package for testing in golang and was amazed that I had not heard about it before now. If you write commnand line applications, sometimes it would be useful to test what your application is outputting to the console. This is where testing by Example from the golang toolbox is very useful. https://blog.golang.org/examples Sample Application code Here is simple sample application: Read On →

Simple Golang HTTP Request Context Example

Intro For the longest time I have wanted to see an easy to understand example (read newbie) of how to add data to the “new” golang 1.7 request context object. Since I wasted more than 2 days looking for something like that and never really found anything I decided to figure it out from the docs and write something up for others like me that wanted something simple. The example below is trivial and does not put anything useful in the context it’s just there to get your own juices flowing. Read On →