Testing in GoLANG

Coming from a ruby background and having done a lot of TDD using the sweet RSpec framework prompted me to look out for the testing frameworks available in Go.

Go comes with a default testing library called “testing”.

First of all, we need to create a _test.go file in the same folder as the file you want to test. Both the files should also be in the same package. Inside the _test.go file, any function-name starting with Test will be tested. Such functions are expected to take a pointer to testing.T.

Here we use expected and actual values to evaluate the condition.

To run the test use the command go test in the folder where this file resides, you should see that all tests are running. On running the test we get a PASS message.

Test written in this way lacked the verbosity of typical Rspec Unit Test. To improve the situation there exist options like "Testify".

It is a assertion based testing framework. It includes packeges for:

  1. Easy assertions
  2. Mocking
  3. HTTP response trapping
  4. Testing suite interfaces and functions

The above spec demonstrate an example of assertion based test using assert package in testify.

There are other frameworks available too like "Ginkgo" which is a BDD testing framework with functionalities very similar to Rspec.

Prashant Mittal is an engineer at C42 Engineering. If you liked this post, please consider...


It's About Time

The date and time is important for an application which is used in different timezones. For example, consider what happens if a user from India enters some data with its own local timestamp at 5:30 a.m and a user from U.S.A wants to see the data. If the timezone is not changed then time which is entered is in the future for the user in U.S.A.

Before getting into actual coding to handle date and time, let us understand the time terminology:

  • UTC - The reference time.
  • Standard Time - The local time without a daylight saving time offset. For example, in India, standard time(IST) is UTC+5:00.
  • Daylight Saving Time(DST) - The local time with a daylight saving time offset. This offset is typically one hour, but not always. It is typically used in most countries away from the equator. In India, daylight saving time is UTC+05:30

Ruby provides two special Libraries to handle time and date:

  1. Time: It is usually sufficient for everyday programming needs because the class uses the time provided by the operating system. It may not be able to display dates before the year 1970 and after the year 2038 and is based on library time.
  2. Date: It consists of two classes Date and DateTime

There are many solutions to managing the timezones and one of them is to store the time in a standard format like UTC and converting it to the user's time-zone.

The code given below can display all possible date-time formats:

Praveen Shukla is an engineer at C42 Engineering. If you liked this post, please consider...


Crystal for rubyists

For last few weeks I have been exploring a relatively new language (about 2 years old) Crystal (hasn’t hit 1.0 as of today). Crystal is inspired by Ruby and it has Ruby like syntax that we all love. It is a statically typed language, so you won’t have runtime bugs since the compiler will catch type related bugs during compilation. 

Crystal is very efficient in compiling code to native code which can be distributed as binary.

Crystal has a modern standard library which has implementations of markdown and websockets baked into it. 

And while we are in introduction territory, it’s important to know that Crystal is self-hosted. That means 

The Crystal is 100% written in Crystal. 

Crystal Basics

  • Everything is a object just like Ruby.
  • There are two types of objects, Values and References.
  • Value objects includes Number, Bool, Nil , Char, Struct, Tuple and Pointer.
  • Rest all objects are References including String, Array and Hash.
  • Value objects are passed by value and Reference objects are passed by reference.

Now that we’re over with the introductions, let’s dig a little deep into language core constructs and concepts.

Global Type Inference

Crystal determines the type of objects and return types of functions using mechanism called global type inference. The process includes traversing the whole AST and assigning type to each node based on type expression it represents. 

Let’s catch up on curious case of Union Types. 

In above code snippet the type of variable name is determined by type inference. The AST node representing first conditional expression will have type of String associated with it while the AST node representing second expression will have Boolean type associated with it. The variable name hence will have a union type String | Boolean associated with it.

Let’s take another example of Union Type.

foo = ["foo", false, 1, "bar"] typeof(foo) #=> Array(String | Int32 | Bool)

In above code variable foo will have type Array(String | Int32 | Bool).

Moving on.

Method Overloading

Method overloading has three different criteria.

  • Number of arguments.
  • Types of arguments.
  • If methods accepts a block or not.

Let’s take an example of addition.

The last line in above code snippet will throw below error.

no overload matches ‘String#+’ with types Int32
Overloads are:
 — String#+(other : self)
 — String#+(char : Char)

During the compile time Crystal determines all the possible overloads and throws errors if type of arguments supplied doesn’t match the possible argument types available in overloads. 

You can also pass type of arguments in methods like this

Let’s try to fix the String and Number overloads situation.

This works perfectly fine since now we have a overload that can handle argument of type String and Number.

Moving right along!


Structs are 

  • stack allocated
  • passed by value since Struct inherits from Value.

while class is heap allocated and passed by reference since it inherits from Reference. These features makes struct more suitable for defining a type in. For example if you want to build a vehicle, the implementation would look something like this.

This is just a very brief overview of crystal. This is missing many concepts like macros, generics, pointers, slice and much more, which I’ll try to cover in next blog.

You can help

  • by contributing to crystal.
  • by funding crystal's development and help it become production ready at bountysource.

Vijay Dhama is an engineer at C42 Engineering. If you liked this post, please consider...



Setting up certain tasks to be run as a daemon can be done using Rake. The actual logic to be executed can be invoked by a Rake task. That Rake task can then be daemonized using the start-stop-daemon command. If required, the PID of that Rake task can be stored in a PID file which can be used by a monitoring service like Monit.

Sample Rake task that executes some business logic in a Rails app

If the command to execute the rake task and any environment variables are stored in a startup script, then the startup script can be executed to create the daemon. The command to start the daemon will be something like :
"/sbin/start-stop-daemon -S --quiet --pidfile #{path to file to store the PID in} --exec #{path to startup script} -b"
The command to stop the daemon will be :
"/sbin/start-stop-daemon -K --pidfile #{path to file to store the PID in}"

Shishir is an engineer at C42 Engineering. If you liked this post, please consider...


When to use the command pattern

Given the problem statement

Design a system to emulate cricket. A captain should be giving some commands and depending on the command players are supposed to act on it.

It might at first look like the Command Pattern to novice programmers who just discovered the Command Pattern. Today I will recount an episode of where I had done the same. The impatience combined with carelessness made me think that this problem could be easily solved using the Command Pattern. The classes I built were as follows.

Now using the Executer class we can make the Bowler or Batsman execute according to his/her position in the team. This is called a Command Pattern and is mainly used at situations like this where you have to execute the commands in a particular order. But I can implement the same using the Strategy Pattern as well. For example, the left orthodox medium pace bowler can be the base class from which Glen Mcgrath is inherited from. Using different such types, we can implement the Strategy Pattern.

So then why use a Command Pattern?

This article about Command Pattern says -

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Here in the Cricket class we need not have logging or undoing operation. Imagine a batsman going to the boundary line and bringing the ball back and giving it back to the bowler!

So where exactly are command patterns not an overkill?

Examples of Command Pattern:

  1. Computer games where each command object is sent over the network to be executed on the server machine.
  2. Any application which uses undo/redo operations.
  3. A simple application using a command pattern would be a calculator.

It is also necessary to understand that it is always better to evolve a pattern out of code instead of imposing a known pattern on it. Design is always an art and in art it is always good to be a Picasso rather than a copycat Picasso.

Anugrah is an engineer at C42 Engineering. If you liked this post, please consider...

  • following @Anunymouze on twitter
  • subscribing to this blog