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. I’m becoming very familiar with gitlab and gitlab-ci.yml and wanted to discover what AWS had to offer. ...

March 31, 2021 · 1 min · Nolan

Mocking Http requests in go

Mocking http retries behavior As we know, http requests can fail for a large variety of reasons, client side or server side (unauthorized, timeouts etc). On the client side a common approach is to retry request by increasing the waiting time between retries. Is this scenario be easily unit test? Unit Testing In golang there is this cool library Simple mocking of an http endpoint: func TestMockHttpResponse(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() httpmock.RegisterResponder("GET", "http://example.com/articles", httpmock.NewStringResponder(200, "my articles").String())) resp, err := http.Get("http://example.com/") // return http 200 ... } But there is more, we can actually easily mock retries like this below: ...

April 10, 2023 · 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() != "fake return output" { t.Fatalf("failed" + 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. It can also work on multiple files, by using a wildcard, as seen in the documentation ...

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: FROM amazonlinux RUN yum -y install bzip2 wget gcc make tar COPY install_gpg.sh . RUN chmod +x install_gpg.sh && ./install_gpg.sh ENV 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.17 - name: Install go migrate run: | curl -L https://github.com/golang-migrate/migrate/releases/download/v4.15.1/migrate.linux-amd64.tar.gz | tar xvz; sudo mv migrate /usr/bin/migrate - name: Run migration run: migrate -path db/migration -database "postgresql://root:test@localhost:5432/sqljson?sslmode=disable" -verbose up - run: go test -v ./... Adding MySQL in Github action I wanted to do the exact same for MySQL but it’s even simpler because mysql is already present, you just need to start it. ...

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.dev/src/bytes/buffer.go#L297 ...

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. By doing so we can easily add options without breaking existing code. ...

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.Replace(s, "b", "c", -1) } func TestFiles(t *testing.T) { tests := []struct { testName string oldConfigVal string newConfigVal string err error }{ {"Testing First config", `a:b`, `a:c`, nil}, {"Tesing second config", `e:f`, `e:f`, nil}, } t.Run("Test config replaced correctly", func(t *testing.T) { for _, tc := range tests { t.Run(tc.testName, func(t *testing.T) { res := rep(tc.oldConfigVal) t.Log(res, tc.newConfigVal) if res != tc.newConfigVal { t.Errorf("unable to read file") } }) } }) }

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.MarshalIndent(d, "","\t") if err != nil { panic(err) } fmt.Printf("%s\n", result) } Decoding JSON package main import ( "encoding/json" "fmt" ) type Data struct { Id int `json:"id"` List []string `json:"list"` NotPrint string `json:"-"` } var b = []byte(` { "id" : 123, "list": ["a", "b", "c"] } `) func main() { var d Data valid := json.Valid(b) if valid { json.Unmarshal(b, &d) fmt.Printf("%#v", d) } }

September 25, 2021 · 1 min · Nolan