SOLID Go

SOLID is a famous cargo-cult that is used to poke “bad” code during code review. Jokes aside there are some solid, pun intended, ideas within SOLID. This post is yet another attempt to dismantle this set of principles and understand them better. The SOLID stands for(pasted from wiki): S ingle-responsibility principle: “There should never be more than one reason for a class to change.“In other words, every class should have only one responsibility O pen–closed principle: “Software entities … should be open for extension, but closed for modification.” L iskov substitution principle: “Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.“See also design by contract. I nterface segregation principle: “Many client-specific interfaces are better than one general-purpose interface.” D ependency inversion principle: “Depend upon abstractions, [not] concretions.” S single responsibility Out of all these, only first one makes sense right away. If you are not a complete beginner, you won’t create a struct that does shipping calculation, checks weather condition, sends push notification and orders pizza. Don’t you? ...

June 6, 2022 · 3 min · 558 words

Notes on Garbage Collection in Golang

Garbage Collection is a process of freeing memory that is allocated and contains some data that is not being used. Notes: by doing escape analysis GC mechanism decides what goes to heap and what stays on stack use -gcflags '-m' flag to get escape analysis info, e.g. go run -gcflags '-m' main.go another way to look into what GC is doing during runtime is to run program with GODEBUG=gctrace=1 GC runs consurrently with main program running main running program is called “mutator” Golang Garbage Collection uses “tricolor algorithm” otherwise known as tricolor mark and sweep algorithm. GC represents data in the heap as graph of elements/objects channels are also garbage collected when they become unreachable, even if channel is not closed Sources: ...

June 5, 2022 · 1 min · 141 words

Simple case of profiler usage in golang

So I was solving Construct Binary Tree from Inorder and Postorder Traversal and I got myself more less working solution that got accepted. But it was not the fastest one. But then I though — how do I profile buildTree function and see what actually takes time in my program? Golang has an excellent set of tool available, just check an output of go tool. One of the items in a list you’ll see is pprof which is a Golang profiler. ...

December 28, 2021 · 32 min · 6674 words