IT notes

//+build disable

In go if need to sketch something and end having func main() in two files, you could ignore building one by adding at the top of the file: // +build disable This is a build constraint. A build constraint, also known as a build tag, is a line comment that begins: // +build

Golang HTTP Timeouts

If this: http: Accept error: accept tcp [::]:80: accept4: too many open files; retrying in 40ms Check that your go server is using proper timeouts, example: srv := &http.Server{ Addr: ":80", Handler: router, ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, } log.Fatal(srv.ListenAndServe()) https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/

Go Benchmarking

To exclude all the tests and just run the benchmarks: go test -run=^$ -bench=. Profile: go test -cpuprofile=cpu.out -benchmem -memprofile=mem.out -run=^$ -bench= Using pprof: CPU: go tool pprof <your-binary>.test cpu.out MEM: go tool pprof -alloc_objects <your-binary>.test mem.out Go Tooling in Action

Webcrypto and Go

Encrypt on client side using javascript WebCrypto and decrypt on backend using go: javascript code: async function aesGcmEncrypt(plaintext, password) { const pwUtf8 = new TextEncoder().encode(password); // encode password as UTF-8 const pwHash = await crypto.subtle.digest('SHA-256', pwUtf8); // hash the password const iv = crypto.getRandomValues(new Uint8Array(12)); // get 96-bit random iv const alg = { name: 'AES-GCM', iv: iv }; // specify algorithm to use const key = await crypto.subtle.importKey('raw', pwHash, alg, false, ['encrypt']); // generate key from pw const ptUint8 = new TextEncoder().

dsymutil golang

Go executables broken in macOS 10.12.X When compiling and executing this may happen: $ zsh: killed ./main Temporary fix: $ ln -s /usr/bin/true /usr/local/bin/dsymutil

nil pointer

To check that x implements y, example: package main import ( "fmt" "io" "os" ) var _ io.ReadCloser = (*os.File)(nil) func main() { fmt.Println("Hello, playground") } https://play.golang.org/p/hoi5_GQEpD To check that os.File is an io.ReadCloser: var _ io.ReadCloser = (*os.File)(nil) package main import ( "fmt" "io" "os" ) var _ io.ReadCloser = (*os.Process)(nil) func main() { fmt.Println("Hello, playground") } https://play.golang.org/p/7uHmjn3ggT The error in this case would be: main.go:9: cannot use (*os.