Use utf8.RuneCountInString(s) to get the actual count of unicode codepoints in the string vs the byte length of the string which you’d get if you simply use len(s).
Looping through characters of a string (not just bytes):
Writing a isPalindrome function that respects unicode characters:
Day 1 Part 3
resp.Body is of type io.ReadCloser, which is a custom type that implements both io.Reader and io.Closer interfaces
io.Copy(to, from) — different from other langs, possibly to imitate to = from analogy?
log.Fatalf() can be used to print some message and quit (by calling os.Exit(1), which it does for you)
resp.Header is not a map, even though it looks like one
If we want to ignore some returned value, we can use _
Fun fact: api.github.com returns a prettified JSON response depending on the user agent. It does this for curl and browser agents.
By default, Go’s JSON decoder will convert JSON number to float64
JSON arrays are mapped to []any
JSON objects could be mapped to either map[string]any or struct
JSON decoding/encoding:
JSON → io.Reader → Go: json.Decode
JSON → []byte → Go: json.Unmarshal
Go: io.Writer → Go: json.Encode
Go: []byte → Go: json.Marshal
url.PathEscape can be used to escape part of a URL path
Day 1 Part 4
We don’t want to keep file descriptors open, but we also want to close them only at the end of the current function. We can use defer file.Close() to have this get executed before the function returns