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

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

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

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

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.Println(i) } Return statements Return statement can be specified in function definition E.g: ...

April 5, 2021 · 2 min · Nolan