3 ms·
> The usual way to get HTTP Headers using Go cannot distinguish between an empty header value and no header at all HTTP headers in Go are maps, which have a bu
by cle 2y ago
> The usual way to get HTTP Headers using Go cannot distinguish between an empty header value and no header at all
HTTP headers in Go are maps, which have a built-in mechanism for checking key existence, which distinguishes b/t empty and missing. No nils involved.
if vals, ok := headers["Content-Length"]; !ok {
// no content-length header was passed
}
- zaptheimpaler 2y agoIt looks like this idiom is specifically for maps, but the recommended way (by their own docs) to access Headers is using the `.Get` method, which canonicalizes header names before looking at the map. This method predictably does not support the idiom, because they made a stupid special case idiom that applies only to maps instead of using an Option type that would work in all cases, including custom types that might have similar requirements. Like this doesn't work: nonexist, ok := req.Header.Get("X-Custom-Header")
- cle 2y agoSounds like you're actually just mad that it didn't have generics from the beginning. That horse has been beaten to death for years. I don't think it's fair though to characterize different points on a tradeoff curve as "stupid". If you're mad that you're forced to use a language with inappropriate tradeoffs for your context, then the right person to blame would be whoever picked it for your project to begin with.