Examine my beginner golang code for idiomatic correctness?

xuanbao · · 614 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi! I&#39;ve recently started learning go by submitting PRs to projects that I had an interest in, and felt I&#39;d seen enough to start my own. I&#39;ve put together this set of <a href="https://github.com/jangie/goloadbalancers" rel="nofollow">loadbalancer algorithms</a> as a result along with some tests. As I don&#39;t do this kind of stuff at work, I&#39;d love to have some comments as to whether this looks like &#39;proper&#39; go and for anything else you can think of!</p> <hr/>**评论:**<br/><br/>driusan: <pre><p>I just had a quick look at the main server.go, and it&#39;s not bad for a first stab at Go, but there&#39;s a few minor nits:</p> <ol> <li>Why are you import/discarding &#34;net/http/pprof&#34;?</li> <li>You&#39;re discarding errors instead of handling them. Instead of returning <em>testHarness, you could return (</em>testHarness, error) and then return the err where applicable instead of ignoring it.</li> <li><p>Code like this is unnecessary:</p> <p>var tdrr = testHarness{ next: rebalancer, port: 8096, } return &amp;tdrr</p></li> </ol> <p>You can just do:</p> <pre><code>return &amp;testHarness{ next: rebalancer, port: 8096, } </code></pre> <ol> <li>You&#39;re not consistent with if you range through the balancees and then return a *testHarness, or declare a *testHarness and then range through the balancees, then return the *testHarness in getRoundRobinHarness/getDynamicRoundRobinHarness.</li> </ol></pre>skidooer: <pre><blockquote> <p>Why are you import/discarding &#34;net/http/pprof&#34;?</p> </blockquote> <p>Probably because that is how it is meant to be used: <a href="https://golang.org/pkg/net/http/pprof/" rel="nofollow">https://golang.org/pkg/net/http/pprof/</a></p></pre>jangiegb: <pre><p>2 I&#39;ll make sure to propagate errors up! 3 is dumb of me, I&#39;ll take care of that.</p> <p>I&#39;ll also take care of the inconsistencies, thanks!</p></pre>alexflint: <pre><p>Looks like very clean code imho. In jsq.go line 120-121, it&#39;s quite common to instead use</p> <pre><code>http.Error(w, http.StatusBadGateway, &#34;msg...&#34;) </code></pre></pre>jangiegb: <pre><p>Ooo, I haven&#39;t seen that before, will be changing, thanks!</p></pre>alexflint: <pre><p>Personally I would have the balancer constructors take in a <code>[]url.URL</code> rather than <code>[]string</code> since then you can handle any URL parsing errors outside the constructors. Another option would be to have the constructors return errors, but I think it&#39;s always nice to have functions that can&#39;t fail (and hence do not need error return values).</p></pre>jangiegb: <pre><p>Good call! I&#39;ll be changing those tonight hopefully :)</p></pre>kpurdon: <pre><p>You should also check out the <a href="https://invite.slack.golangbridge.org/" rel="nofollow">gophers slack</a> specifically the #reviews channel. </p></pre>

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

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