Go Performance – A Checklist

Avoid Global Mutex

Don’t use a global mutex, keep it tied to structures. Go doesn’t have GIL and it is tempting to create a global mutex to simulate a similar behavior but then why use Go in the first place? You can tie a mutex to a structure to keep its impact as low as possible.

Avoid Defer Functions

Deffer functions are significantly expensive than the non-deferred ones. In the case of static functions, an improved version is compiled but it is still slower than direct functions.

Avoid recreate, reuse Variables

It is a usual way to create a variable in a loop. It takes an extra CPU cycle as well as it would keep Garbage Collection busy. Instead, use sync.Pool

F`k it – Fix it – Psychology

I have just created a page Hire Me and told few friends and one asked me to come over and discuss a few projects his team is working where I can assist. Every single one of them was in Dot Net except one in Go. He is helping a renowned trader who has chosen to stay anonymous. A project is having code name OTAMIA.

Too Hot to Touch!

He stated that because of some inconsistency in an official Client of Kite Connect, the project is becoming annoying and unnecessary harder. His approach was not to touch the official API but use some duplicate structures to fix the issue or some conversions. He didn’t touch the official code but then again it wasn’t new. I created a framework on which every single Shopify App was build in SpiceGems and there, everyone worked on the framework decided not to touch what I wrote and this made a huge pain for everyone. I asked them why – they just said it is official (part of a framework)! My response was ‘F`k it Fix it’

F`k it – Fix it

The whole job of mine is to save time and pain. So we fixed the following issues or added feature (so far)

  1. Turned two packages into single – R.I.P compatibility.
  2. This enabled us to use a single structure for both packages.
  3. The error was coming as nil even when there was one – we fixed it.
  4. Data in converting JSON to STRUCT was giving blank (0000-00-000) dates – we added another known date format.
  5. We added a new flag to enable OI to be fetched.

All of the above changes were made within 2 hours but this removed all the unnecessary conversion, custom structure for proper JSON conversion and managing time column in our project. All of this so far had taken more than a week.

Be Brutal – Consultant’s Principal

Coding must be enjoyable and just because something is coming under the tag “Official” it doesn’t mean that there is no scope of improvement or you got to use that in the same way. If I can tell my team to f’k my framework, I can tell to f’k anyone’s. Knowledge and Tech both are evolving and we are all learning. Wherever you can save time and pain – just go ahead. Although, sometimes initial pain would save a lot of pain and time in the future. You will have to make a wise choice.

Open-Source KiteAPI

It was almost impossible for my client to let us make it opensource but he agreed and we have a code over here under MIT license.


Cannot assign to struct field in map

Understanding Maps in Go

Have you ever come across an error “Cannot assign to struct field in map”? So what is the issue here?

Imagine that we have a structure:

package main

type Product struct {
	Name  string
	Image string
	Cdn   string
	Price float64

func main() {

	products := make(map[int]Product)

	products[0] = Product{"Blue Diamond Book", "https://localhost/split/bdb.png", "https://localhostcdn/split/bdb.png", 120.00}
	// Problem
	products[0].Price = 121.00


The Error

If we have to reassign value to a variable like this:

products[0].Price = 121.00

We’ll get an error: “Cannot assign to struct field in map”

Pointer Map Problem in Golang

The reason for this panic is, the map is a pointer to value and Go checks the key, is it there or not. If you had used a map, you must have seen:

thisProduct, ok := products[0]

thisProduct” will get the Product structure’s object and “ok” will get a bool value, that this key exists or not

So we have to assign the pointer to a variable.

// Solution
	if thisProduct, ok := products[0]; ok {
		thisProduct.Price = 121.00
		products[0] = thisProduct

Now, we can access and reassign the value of the Student structure’s object:
thisProduct.Price = 121.00
After making the required adjustments, this thisProduct can be assigned back to the same pointer.