How to Simple Debugging in Golang

Djohanprabowo
2 min readNov 12, 2020
Image retrieved from wikimedia.org

Golang is one of my favorite language programming. It is very simple and understandable code. Golang is one of the fastest compiling languages. But when we code with carelessly, it can make problem to future. In this article, I will share about one of experiences with Golang in real job :)

Introduction Problem

In my workplace we have many services with Golang for backend. One of services that is handled by my team had one problem. When reloading service, CPU is always high and time for reloading is long about three minutes. It is very weird, because the preparation to init it is very simple. The preparation is only connect with redis, database, and init class. This problem hinders work. Because if we want changes some config rapidly, it can harm the server. So we must erase the obstacle. It’s very challenging for me and my team.

Let’s Beat Obstacle

  • The first we must knowing about the issues. We must be able to decide about tools or way to knowing the problem. So in this case, we know one of the problems is long execution time. So, we decide using time.since(). Because it can easy to trace which execution is taking very long. We give code from main function first. After we run the application we got info about execution every process. We can easily take investigate child process with time.since until we know the main problem. So it is very simple to debug. Example pseudocode
start := time.Now()
InitModule1()
log.Println("Execution InitModule1 :",time.Since(start).Seconds())
start = time.Now()
InitModule2()
log.Println("Execution InitModule2 :",time.Since(start).Seconds())
start = time.Now()
InitModule3()
log.Println("Execution InitModule3 :",time.Since(start).Seconds())
start = time.Now()
InitModule4()
log.Println("Execution InitModule4 :",time.Since(start).Seconds())
  • Finaly I got the problem, which process is taking a very long time. It’s very simple code and I never expect this is the root cause of the problem. The code is very tidy but it’s very not efficient. And I didn’t expect that config containts more than 20 configs type with every types containts more than 2 mode and the function validation had many iteration. Example pseudocode
  • After we know the problem I try improvement with using type data map. First we looping config.Available to construct map data. After that can decrease our duplicate looping. Example pseudocode

Result

After doing improvement, we got achieve execution for reloading service only 2s and the cpu only spike to arround 1–2% only. This is very satisfying. because this really makes maintenance service well without having to wait long when we want to change config rapidly :)

Conclusion

We must be carefull when code using iteration case. Because when our code is not efficient it can make problem in features. Some of the tips for this case are using time.since very helping to decide which process is taking too long and using map data can improvement our iteration.

--

--