Reduce Go Binary Size
Always a hot-topic in Go circles is binary size of applications. In this post we will show you how you can reduce the binary size produced from go build for leaner, slimer builds. In short, adding the ldflags shown below will reduce the size of the binary produced. It does this by removing some perhaps non-critial parts (shouldn’t affect execution, only debugging potentially). -s Will turn off the symbol table, so you won’t be able to use commands like go tool nm.
Using Iota for a List of Constants (Like Enums)
Enums have been used in many programming languages in varying forms for many years. But if you’ve found this post, then you are most likely looking for how to do this in Go (golang). In Go, there’s a handy feature called iota which is a running list of ever-increasing constants. They are a consise way of presenting a list of things in a “don’t repeat yourself” way. For example if you need to define days of the week using contants, you could:
Remove All Non-Alphanumeric Characters
We often need to remove symbols and special characters from the strings we’re using (especially with currency!). This post shows how you can keep the letters and numbers, but remove any punctuation, symbols, grammar, etc. For example, if a user types in “$1,000” you can turn it into “1000”. We use the regexp package to do this, first building a regex with .Compile() then running the string through that regex with .