dongyan1974
2016-06-08 10:19
浏览 44
已采纳

golang代码组织:应该将仅与一个功能相关的自定义错误类型放在哪里?

I have just started working on my first golang project and really like the idea of returning custom error types from functions and using type assertion in the calling code, to check for specific errors. I find this solution cleaner than always comparing error messages.

My only question is: where do you best put these custom error types?

Say a number of custom error types is only used (returned) by one utility function, should they go in the same package as the function? Should I group them somehow? Or maybe there's a better way of doing this kind of thing..

图片转代码服务由CSDN问答提供 功能建议

我刚开始从事我的第一个golang项目,真的很喜欢从函数中返回自定义错误类型并使用 在调用代码中键入断言,以检查特定的错误。 我发现此解决方案比总是比较错误消息更干净。</ p>

我唯一的问题是:您最好将这些自定义错误类型放在哪里?</ p>

说一句 自定义错误类型的数量仅由一个实用程序函数使用(返回),它们应该与该函数放在同一程序包中吗? 我应该以某种方式将它们分组吗? 或者也许有更好的方法来做这种事情。</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题