<p>hey everyone! first time poster in this subreddit. I've been working with go for about 4 months now and absolutely love it.</p>
<p>Anyways, I'm working on a relatively complex web application back-end and have gotten a little concerned that in places where I need to return errors, I am using string literals for the string value which I'm starting to think could be a major headache later:</p>
<pre><code>if roles.IsAdmin(&user) != true {
return fmt.Errorf("UNAUTHORIZED")
}
</code></pre>
<p>So I'm wondering: would it be bad practice to define in my application an <code>errors</code> module and define every application error string in there? E.g:</p>
<pre><code>import "github.com/me/myproject/errors"
if roles.IsAdmin(&user) != true {
return fmt.Errorf(errors.ErrUnauthorized)
}
</code></pre>
<p>Or is the correct approach to have the errors defined in the module of which they could be produced, e.g:</p>
<pre><code>if roles.IsAdmin(&user) != true {
return fmt.Errorf(roles.ErrUnauthorized)
}
</code></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传