←back to thread

Hyrum's Law in Golang

(abenezer.org)
107 points thunderbong | 2 comments | | HN request time: 0.468s | source
Show context
hambes ◴[] No.42202204[source]
Solution to the specifically mentioned problem: Don't use string-based errors, use sentinel errors [1].

More generally: Don't produce code where consumers of your API are the least bit inclined to rely on non-technical strings. Instead use first-level language constructs like predefined error values, types or even constants that contain the non-technical string so that API consumers can compare the return value againnst the constant instead of hard-coding the contained string themselves.

Hyrum's Law is definitely a thing, but its effects can be mitigated.

[1]: https://thomas-guettler.de/go/wrapping-and-sentinel-errors

replies(6): >>42202257 #>>42202260 #>>42202261 #>>42202464 #>>42202551 #>>42202608 #
1. karel-3d ◴[] No.42202608[source]
Using string error comparisons was the only way to do this few years ago; and Go has a backwards compatibility promise.
replies(1): >>42202634 #
2. ◴[] No.42202634[source]