Back to blogging

Hugo Blogging I’ve tried different blog platforms over the years: ghost, wordpress. Hugo is very popular nowadays, so I wanted to try it out. Moreover, I came across that theme that looked cool: https://themes.gohugo.io/hugo-papermod/ I don’t have a specififc goal in mind for that blog, it’s a just a cool way for me to look back and see what I’ve been doing. AWS Amplify I also wanted to try out AWS Amplify....

March 31, 2021 · 1 min · Nolan

Monkey Patching in Golang

Monkey Patching As I’ve seen monkey patching is not advise in golang but it is possible here is a simple example. Code to test: package main var version = func(test string) string { return "true version" + test } func getVersion() string { return version("test") } Test package main import ( "testing" ) func TestVersion(t *testing.T) { oldfunction := version defer func() { version = oldfunction }() version = func(name string) string { return "fake return output" } if getVersion() !...

September 27, 2022 · 1 min · Nolan

Golang, Adding static content in binaries

Embed directive When building a binary, if you load config files, they won’t be added to your binary automatically. (The embed package was added in golang 1.16 before then some external libraries could be used). You have to specifically tell the compiler to add them, by using embed. import _ "embed" //go:embed my_config.yaml var config string From what I’ve seen, it cannot be done in a function it has to be global....

September 9, 2022 · 1 min · Nolan

Bulding GPG on Amazon linux from source

Bulding a newer version of GPG on AL2 Amazon linux 2, based on centos 7 is using a older version GPG making it incompatible with some projects see this thread. And so one solution, is to build a newer version of GPG from source. Here is the github repo: FROMamazonlinuxRUN yum -y install bzip2 wget gcc make tarCOPY install_gpg.sh .RUN chmod +x install_gpg.sh && ./install_gpg.shENV PATH="/var/src/gnupg2/gnupg-2.2.15/dest/bin:$PATH"And the script compiling from source:...

August 6, 2022 · 1 min · Nolan

Adding service DB in Github action

This is so useful for testing purposes, plus it’s getting easier to do so. Let’s take a look for PostgreSQL and MySQL. Adding PostgreSQL in Github action name: Build on: push jobs: build: runs-on: ubuntu-latest services: # Label used to access the service container postgres: # Docker Hub image image: postgres:13 # Provide the password for postgres env: POSTGRES_USER: root POSTGRES_PASSWORD: test POSTGRES_DB: sqljson # Set health checks to wait until postgres has started options: >---health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 ports: - 5432:5432 steps: - uses: actions/checkout@v2 - name: Setup go uses: actions/setup-go@v2 with: go-version: ^1....

March 5, 2022 · 2 min · Nolan

Sending a slow request

Sending Slow request in golang I thought it was a cool snippet that I wanted to share and keep (AKA slow loris attack). The http Post method takes an io.reader that could be overridden. It could be used to test how your web application reacts in those situations, does it hangs? timeouts? Http Post definition: func (c *Client) Post(url, contentType string, body io.Reader) (resp *Response, err error) Taking a Read function as example from https://go....

February 23, 2022 · 1 min · Nolan

Using Variadic in Go

Nothing new Variadic arguments are present in many many languages. In golang the syntax is like so: package main import "fmt" func addNubers(nums ...int) int { s := 0 for _, n := range nums { s += n } return s } func main() { res := addNubers(1,2,3) } In python: def func(a, *t): for e in t: print(e) func(1,2,3,3,4) Usage in go In go variadic functions are used to make configuration easily extendable, without changing existing interface....

October 3, 2021 · 1 min · Nolan

Go testing

Testing in golang Simple test in go package main import "testing" func add(a, b int) int { return a + b } func TestAddingNumber(t *testing.T) { res := add(1, 7) if res != 8 { t.Errorf("Sum is incorrect, got: %d, expected: %d", res, 8) } } Run tests: go test -v ./... Avoiding repeating tests Instead of duplicating code for similar tests, there is a different way to test. package main import ( "strings" "testing" ) func rep(s string) string { return strings....

October 2, 2021 · 1 min · Nolan

Golang Encoding/Decoding JSON

Encoding JSON Encoding Decoding is not as straight forward as other languages as it needs to be mapped to a struct. Or a generic map[interface] can be used if you don’t know the type in advance. package main import ( "encoding/json" "fmt" ) type Data struct { Id int `json:"id"` List []string `json:"list"` NotPrint string `json:"-"` } func main(){ d := Data{Id: 123, List: []string{"abc", "b"}, NotPrint: "This won't print"} result, err := json....

September 25, 2021 · 1 min · Nolan

Go templates

Templating with Sprig Templating can be useful, for instance it is heavily used in helm. Below a simple example to get started, sprig is just library on top of golang templating that provides extra functions. package main import ( "os" template "text/template" "github.com/Masterminds/sprig" ) var temp = ` start {{ .Title | repeat 2 | indent 2}}{{ .Text }}{{ first .List }}end ` type Values struct { Title string Text string List []int } func main() { t, err := template....

September 24, 2021 · 1 min · Nolan