3 ms·
How does go's type system allow the "exactly one of these two" case? What's the syntax?
by kadoban 5y ago
How does go's type system allow the "exactly one of these two" case? What's the syntax?
- BobbyJo 5y agoI'm not sure what you're asking. I was pointing out that if you need to enforce a return result that will have either a value or an error, you can simple write a struct/interface that does that. Like, you can create an interface that's read only for the return result like: interface ReadableResult { HasResult() bool Result() whatever Error() error } Then to create the result you have a writable interface for setting values, and enforce the 'only one of the two' semantics from the setters: interface WritableResult { SetResult(whatever) SetError(error) } Struct MyResult{ err error result whatever } .... Implementation here. It's extra code, but it should be a rare necessity, at least in my experience. Res, err := getResult() always worked fine for me.