Defer on exit

Working with go, sometimes you may want to run a deferred action on Exit. Something like: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package main import ( "fmt" "os" ) func main() { // THIS WILL NOT WORK defer fmt.Println("World") fmt.Println("Hello") // some code here... os.Exit(0) } In the above code, World will never get printed. This is because defer is not run on os.

Musashi’s 21 Precepts for Life

Just came across Miyamoto Mushashi, the greatest swordsman to ever live. He also wrote two books, one of which is Dokkōdō (The Path of Aloneness). It comprises of 21 percepts for living life. A lot of them resonanted with me personally, so I’m listing them out here. (Interpretation of these can be very personal to each, so i’ not even going to try.) Accept everything just the way it is.

Populating go vars at build time

When we build go executables, one common command is the version command. It should tell you the commit, version number etc of the released executable. However, we don’t want to hardcode these in code. We want to populate these variables at build time. Here is a common pattern that will let you do this. Let’s assume you have a main.go that imports a package version and prints out the output of version.

Init function in modules

A Go package can have an init function. This function is called at the time when the package is imported. You can put in any initialization you want here. e.g. 1 2 3 4 5 6 7 8 9 10 11 12 13 package main func init() { fmt.Print("Hello") } func init() { fmt.Print(" World") } func main() { fmt.Println(" People") } The above will print out Hello World People.

.gitattributes

Just like .gitignore, there is another file that you can use in your git repos: The .gitattributes file. You typically place this in the root of your repo. It has lines of the form: pattern attr1 attr2 ... It essentially means that for files that satisfy the regex pattern, apply the attribute attr1, attr2 etc to them. Let’s see some examples. Let’s say our .gitattributes had the line below: *.png binary *.

Books

Some of the books I have recently read. (Items marked with a 👍 are ones that I really enjoyed.) Open: An Autobiography - Andre Agassi - 12⁄2024 (👍) Animal Farm - George Orwell - 11⁄2024 Man and Technics - Oswald Spengler - 11⁄2024 Being Mortal - Atul Gawande - 08/2024 (👍) The War Below: Lithium, Copper, and the Global Battle to Power Our Lives - Ernest Scheyder - 06/2024 (👍)

About

My name is Jimmy and I am engineer / entrepreneur living and working in beautiful Berkeley, CA. I currently work on large scale distributed systems, MLOps, ML Infrastructure, Data Pipelines, Tooling and Platform services / operations / observability. My interests revolve around technology, health, philosophy/spirituality and learning. In a former life, I founded and sold CodeEval. This blog is created via the awesome Hugo framework. It is hosted by Netlify. All the code for this site is available on Github.

Projects

OneHumanRace Trade your time/knowledge/skills for contributions to a charity of your choice. BioHacks Utilities for bio chemists (or others) to play around with DNA sequences. GoCVE A command line client to query Common Vulnerabilities and Exposures (CVE). Text to Morse Code Converts text to morse code and vice-versa. Some characters like the quotation mark(“”) is not supported in this implementation.

Trap in bash scripts

You may be familiar with the “set -e” you typically see in bash scripts. It means that the script should exit on error (default behaviour of bash is to keep going) Recently I needed a script to do some cleanup action. i.e. I wanted the script to exit on error, but I also wanted some cleanup action to be performed. A use case might be that if something fails, then before exiting, ship log files to an S3 bucket for troubleshooting.

Adding multiple certs to an ALB

If you are using AWS and ALBs, you have the ability to add multiple certs to the ALB and terminate SSL there. While it is easy to do via the AWS console, their documentation is not that clear as to how to do it in an automated way. The following is the code snippet, written with troposphere, to show you how to do it. First create a HTTS listener with a certificate: def create_lb_listener_https(alb, default_target_group, param_cert_one): return Listener(‘LoadBalancerListenerHTTPS’, Port='443’,Protocol='HTTPS’, LoadBalancerArn=Ref(alb), DefaultActions=[Action(Type='forward’,TargetGroupArn=Ref(default_target_group))],# Note, only one cert ARN can be specified here, else you will get an errorCertificates=[Certificate(CertificateArn=Ref(param_cert_one)),])