Anders Eknert on Nostr: My new favorite #golang discovery is `cmp.Or` to assign default values. Instead of: ...
My new favorite #golang discovery is `cmp.Or` to assign default values. Instead of:
```
v := version.Version
if v == "" {
v = "unknown"
}
```
do:
```
v := cmp.Or(version.Version, "unknown")
```
With the or-condition triggered by the zero-value for the first arg. Neat!
Published at
2025-01-14 11:56:50Event JSON
{
"id": "cbb5dd0b57cabc920e9583434476fe25b0ca46e9e081c367e7bb34600ef38f2f",
"pubkey": "67c197ecbcb805d1df38c7ebdaf07ec92ba027d8af68a7265eb108c1c4b9f145",
"created_at": 1736855810,
"kind": 1,
"tags": [
[
"t",
"golang"
],
[
"proxy",
"https://hachyderm.io/users/anderseknert/statuses/113826582394790857",
"activitypub"
]
],
"content": "My new favorite #golang discovery is `cmp.Or` to assign default values. Instead of:\n```\nv := version.Version\nif v == \"\" {\n v = \"unknown\"\n}\n```\ndo:\n```\nv := cmp.Or(version.Version, \"unknown\")\n```\nWith the or-condition triggered by the zero-value for the first arg. Neat!",
"sig": "e4c182325d68e932a17f7cb256caed68971a92a9efbf9e4aa056cb237500a006b27c562667f638f1645f6c6491984b080fa8feab9c364ca4297bc677d28d818d"
}