· January 19, 2025 · TIL ·
Feels like I'm endlessly writing some version of "retrieve value, check if empty, set default if empty" flows with Go, particularly when handling optional REST API params/payload values. Other dynamic languages can usually just lean on an OR operator for such things. Didn't think Go had an easier way until today stumbling across a much cleaner option: cmp.Or!
myValue := cmp.Or(params.Values["name"], "Default Name")
Doesn't help dealing with type issues like when grabbing who-knows-what out of interface maps, but dang does it help collapse multiple lines into one! So cool.
Source: Anders Eknert on Mastodon
Previous | Next |
Naming Patterns: Delight and Duct Tape | TIL: Sensible JSON Operators in Postgres 14 |