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.New("todos").Funcs(sprig.GenericFuncMap()).Parse(temp) if err != nil { panic(err) } s := make([]int, 0) s = append(s, 1) v := Values{Title: "The title", Text: "Txt", List: s} err = t.Execute(os.Stdout, v) if err != nil { panic(err) } } Full code is there gitlab ...

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.NewClient(&redisv2.Options{ Addr: "localhost:6379", Password: "", DB: 0, }) } Python It’s impossible in Python for now see this thread ...

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.Println("ok") } wg.Done() } func termination() { os.Exit(1) } func main() { var wg sync.WaitGroup ticker := time.NewTicker(1 * time.Second) defer ticker.Stop() go checkInBackground(*ticker, &wg) time.AfterFunc(time.Duration(10)*time.Second, termination) wg.Wait() fmt.Println("Done as expected") } Not sure if it’s a nice way to achieve that as I’m learning Golang, but I did what I wanted. ...

August 30, 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/*.md It worked! ...

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.com/registry/api/go/go" You can then do a: ...

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.com/t-yuki/gocover-cobertura - go test ./... -v -coverprofile coverage.txt - gocover-cobertura < coverage.txt > coverage.xml coverage: /^coverage:\s(\d+(?:\.\d+)?%)/ artifacts: name: $CI_JOB_NAME/coverage.txt paths: - coverage.xml expire_in: 2 days reports: cobertura: coverage.xml Then in order to get the coverage badge it can be added in the readme or in the repo settings>general>badges. ...

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.jar ENTRYPOINT [ "java", \ "-javaagent:dd-java-agent.jar", \ "-jar", "app.jar", ] And it worked, let’s look into it for golang. ...

April 15, 2021 · 2 min · Nolan

First golang project

Import Function form package I thought it was going to be similar to many languages and I will just have to just do something like: import "mypakckage/Myfuction" Well… no, first it will depend on your GOPATH, which is handled differently depending of your go version. Secondly, In golang exported function must be in Uppercase, otherwise it won’t compile! First module So I chose the easiest that I could find first to structure project by creating a module. ...

April 9, 2021 · 1 min · Nolan