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

PostgreSQL in Docker

Running DB from a dump in seconds I will take this free dataset from postgresql website. docker run -d --name my_db \ -v /Users/nolan/Downloads/french-towns-communes-francaises-1.0/french-towns-communes-francaises.sql:/docker-entrypoint-initdb.d/create_table.sql \ -p 54320:5432 -e POSTGRES_PASSWORD=my_password -e POSTGRES_USER=nolan postgres Connect to DB pgcli -h localhost -p54320 -Unolan nolan@localhost:nolan> SELECT count(*) FROM towns where name like 'Mont%'; Result: count 826

September 21, 2021 · 1 min · Nolan

Using multiple versions of the same library

Golang Looking into dependency management in golang and I was quite surprised that it’s possible to import multiple versions of the same library. However it only works for major version that are considered as a different module. Which must be specified when creating the package E.g module multiversion go 1.15 require ( github.com/go-redis/redis/v7 v7.0.0 github.com/go-redis/redis/v8 v8.0.0 ) package main import ( redisv1 "github.com/go-redis/redis/v7" redisv2 "github.com/go-redis/redis/v8" ) func main() { redisv1.NewClient(&redisv1.Options{ Addr: "localhost:6379", Password: "", DB: 0, }) redisv2....

September 6, 2021 · 1 min · Nolan

Visual Studio

Get a sharable Git URL of a specific line of code When collaborating it’s useful to share the URL that will point to a specific line. To do that you will need the gitlens extension. Then just do cmd + shift + p it will open up the pallet, and then look for Copy remote URL. Here is an example of the result: Click here More to come …

September 3, 2021 · 1 min · Nolan

Tickers

Tickers I was looking into timers and I came across tickers. Basically they allow you to trigger an event at a regular time interval. One simple example could be some kind of monitoring. What’s interesting is that it pushes a Ticker to a channel. Here is a basic example: package main import ( "fmt" "os" "sync" "time" ) func checkInBackground(ticker time.Ticker, wg *sync.WaitGroup) { wg.Add(1) for ; true; <-ticker.C { fmt....

August 30, 2021 · 1 min · Nolan

Limit number of concurrent requests  [draft]

Limiting the number of concurrent http requests I was looking to use gorountine in the context of http requests. More specifically, how to limit the number of concurrent of http call made to a service. Let’s say you are making call to an API that is rate limited.

August 29, 2021 · 1 min · Nolan

Check for misspelled words

Checking for typos & misspell words I came across that project client9/misspell on github that aims to: “Correct commonly misspelled English words… quickly.” So I decided to try it directly for that blog by using a github action. Implementation Github action: name: Go on: [push, pull_request] jobs: build: name: Check for misspell runs-on: ubuntu-latest container: golang:1.15-buster steps: - uses: actions/checkout@v2 - run: |GO111MODULE=on go get -u github.com/client9/misspell/cmd/misspell@v0.3.4 misspell -error misspell blog/content/posts/*....

April 27, 2021 · 1 min · Nolan

Using GOPROXY

Getting dependencies via GOPROXY I recently wanted to use a private registry in a go project. It can be for many reasons: ensure immutability from one built to another ensure avaibility of a build (remember npm author who removed his library and broke half the internet?) get non-public library How it works Quick & Easy It is super easy you just have to add an env variable: export GOPROXY="myprivateserver....

April 19, 2021 · 1 min · Nolan

Golang code coverage badge in gitlab

Adding Golang code coverage badge in gitlab Currently there is no example in gitlab official documentation regarding code coverage for golang. However I found: https://medium.com/@ulm0_/golang-multi-packages-test-coverage-with-gitlab-ci-a7b52b91ef34 https://github.com/t-yuki/gocover-cobertura So I tried them out and it looks to work fine. Live Example I quickly made a test repo: https://gitlab.com/emirot.nolan/go-test-coverage/ To try that out and it work fine ! Here is the gitlab-ci.yml stages: - coveragetest test: stage: coveragetest image: golang script: - go get github....

April 16, 2021 · 1 min · Nolan

Monitoring golang web app with Datadog

Monitoring golang web application with Datadog Adding datdog monitoring for java was a matter of minutes, so I thought for golang it would be even faster. Get the datadog jar add it the the dockerfile entrypoint and that’s it. (assuming datadog env variables are set up correctly in k8s) It will be something like: RUN curl --retry 3 --connect-timeout 3 -Ss -o dd-java-agent.jar https://github.com/DataDog/dd-trace-java/releases/download/v0.78.1/dd-java-agent.jarENTRYPOINT [ "java", \ "-javaagent:dd-java-agent.jar", \ "-jar", "app....

April 15, 2021 · 2 min · Nolan