Best practice for invalid args that the compiler doesn't catch?

Go’s type system can’t represent every parameter constraint. What’s the best way to deal with the ones the compiler doesn’t check?

  • Document it, ignore it in code, let things break as they will?
  • Document it, type check at runtime, panic on failure?
  • Document it, type check at runtime, return an error on failure?
  • Something else?

Panic if it’s a “can’t happen” thing, return an error if it can as it for example depends on user input. IMHO.

Thanks. I meant to ask specifically about errors that result from programmer error in the calling code.

Packages in the standard library also return errors for values with invalid types passed in as far as I’m aware of, take json.Unmarshall as an example.

That’s processing user input. It’s hard to say for sure without example, but it sounds to me like the OP means something more similar to the reflect package, which panics on incorrect usage.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.