Tag tech

Testing with Go

An integral part of the development licefycle is writing tests. Write tests early, write tests often as they say. Go makes it very easy to write tests. The following post will go through the basics of testing with Go. Let’s say we have a program that counts the number of vowels, consonants, digits and everything else within an input string. It may look something like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package main import ( "flag" "fmt" ) // CountChars counts the number of characters in a string func CountChars(str string) (int, int, int, int) { vcount := 0 ccount := 0 dcount := 0 ocount := 0 for i := 0; i < len(str); i++ { switch str[i] { case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U': vcount++ case 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z': ccount++ case '1', '2', '3', '4', '5', '6', '7', '8', '9', '0': dcount++ default: ocount++ } } return vcount, ccount, dcount, ocount } func main() { inputStr := flag.

Introduction to Go Modules

Table of Contents Preface Why Go Modules Philosophy Example Preface All the content for this post was gathered by reading the 7 part blog series by Russ Cox and other notes. There is also an introductory post on the Go blog. I would encourange you to read them as well. Why Go Modules Before jumping into the workings of Go modules, we should first understand why we need it in the first place.

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.

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.


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 *.

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)),])

Docker image tagging in ECR

AWS provides a docker repository called ECR. This is similar to Docker Hub, Artifactory etc and provides a convenient place to place docker images for later use e.g. deployment. ECR supports Docker Image Manifest V2, Schema 2, providing the ability to add multiple tags per image. I recently ran into an issue wherein I tagged an image with multiple tags at build time and then just pushed one tag, thinking all tags would appear on ECR.