application module dedicated to defining error strings

polaris · · 405 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>hey everyone! first time poster in this subreddit. I&#39;ve been working with go for about 4 months now and absolutely love it.</p> <p>Anyways, I&#39;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&#39;m starting to think could be a major headache later:</p> <pre><code>if roles.IsAdmin(&amp;user) != true { return fmt.Errorf(&#34;UNAUTHORIZED&#34;) } </code></pre> <p>So I&#39;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 &#34;github.com/me/myproject/errors&#34; if roles.IsAdmin(&amp;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(&amp;user) != true { return fmt.Errorf(roles.ErrUnauthorized) } </code></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

405 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传