I wrote my first go library! Looking for feedback.

polaris · · 517 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I noticed that one of the features that python&#39;s nltk had that I couldn&#39;t find in go was a rhyming dictionary, so I wrote a pronunciation and rhyming dictionary in go:</p> <p><a href="https://github.com/juanchel/rhymer">https://github.com/juanchel/rhymer</a></p> <p>I&#39;d love some feedback, I&#39;m not sure if I&#39;ve been doing everything the &#34;right way&#34; in go. Any thoughts are appreciated!</p> <hr/>**评论:**<br/><br/>bradfitz: <pre><p>Problem 0: gofmt it.</p> <p>Needs package doc.</p> <p>Make sure your docs look good: <a href="https://godoc.org/github.com/juanchel/rhymer">https://godoc.org/github.com/juanchel/rhymer</a></p> <p>End sentences in periods.</p> <p>Rename NewRhymer to just New. (read Effective Go and https:/golang.org/s/style but especially their naming sections)</p> <p>You have a public constructor returning a private type:</p> <p>func NewRhymer() *rhymer {</p> <p>That is bizarre. Make Rhymer exported (capitalized).</p> <p>Why are there some public functions and also a Rhymer type? Can you configure the Rhymer?</p> <p>Why not all functions or all methods?</p> <p>Doc comments aren&#39;t in complete sentence style: <a href="https://github.com/golang/go/wiki/CodeReviewComments#comment-sentences">https://github.com/golang/go/wiki/CodeReviewComments#comment-sentences</a></p> <p>etc.</p></pre>liyate: <pre><p>Thanks for the feedback! I didn&#39;t realize that docs could be automatically generated from my code.</p> <p>So the Rhymer object is what contains all the pronunciation data and is needed to check words. However, the functions that just reduce words don&#39;t need that data so I made them as functions. Would it be better to make them all methods even if they don&#39;t need anything from the object?</p> <p>Also if I export the rhymer struct, that would mean that I would be able to instantiate rhymer without any data in it, since that&#39;s done in the constructor. Would it still make sense to export it?</p> <p>edit: actually I think I see what you meant about the rhymer object, I went ahead and exported it. Thanks again.</p></pre>alexwhoizzle: <pre><p>If you didn&#39;t want to export the Rhymer type you could also create a public interface that the rhymer type adheres to and return that from your New() constructor.</p></pre>captncraig: <pre><p>This. Public interface/ private struct is a great pattern.</p></pre>anonfunction: <pre><p>Go has a lot of great built in tooling including <code>godoc</code> which can be used to read docs for the standard library or any packages you have locally right from the terminal. Check out this Makefile for some cool stuff: <a href="https://github.com/montanaflynn/stats/blob/master/Makefile" rel="nofollow">https://github.com/montanaflynn/stats/blob/master/Makefile</a></p></pre>anonfunction: <pre><p>There&#39;s a great tool called <a href="http://github.com/alecthomas/gometalinter">gometalinter</a> that runs your code through a series of linters and code quality checkers:</p> <pre><code>go get github.com/alecthomas/gometalinter gometalinter --install gometalinter </code></pre></pre>

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

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