Go

Notes from writing Pegel in Go, mostly about the standard library.

TIL

A TOML decoder can list the keys it ignored

toml.DecodeFile from BurntSushi/toml returns a MetaData, and its Undecoded() lists every key in the file that matched no field. Pegel 0.9.0 uses it to refuse a typo such as intervall instead of ignoring it.

md, err := toml.DecodeFile(path, &cfg)
if err != nil {
	return nil, err
}
if keys := md.Undecoded(); len(keys) > 0 {
	return nil, fmt.Errorf("%s: unknown key %q", path, keys[0].String())
}

Before, a misspelt key fell back to its default without a word, and nobody suspects a default.

TIL

Go’s http.Client waits forever by default

An http.Client without Timeout never gives up on a server that accepts the connection and then sends nothing. The probe hung on exactly that for 40 minutes during testing.

client := &http.Client{Timeout: 10 * time.Second}

Timeout covers the whole request, from dialling to the last byte of the body. For a check, that is the number we want.

Buy Logbuch