Welcome to my personal blog
- This blog will be about every technology related topic that will cross my mind
Welcome to my personal blog
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....
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....
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() !...
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....
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:...
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....
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....
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....
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....
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....