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

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

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

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

First few lines in golang

Intro I’ve been casually looking into the go language without trying it much. Now it’s time to write so go code :) Golang Looping First discovery there is no while keyword in golang, I was pretty surprised I thought I was missing something. I then look in the spec and there is only for. I like that they only have limited number of keywords. i := 0 for i < 10 { fmt....

April 5, 2021 · 2 min · Nolan