Which is most idiomatic?

polaris · · 316 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m writing a library for the first time and I think I did something wrong in exposing the functions. Right now it behaves like so:</p> <pre><code>import ( &#34;mylib&#34; ) func main() { mixer := mylib.NewMixer(&#34;ingredients&#34;) mixed := mylib.MixThing(&#34;data&#34;, mixer) unmixed := mylib.UnMix(mixed, mixer) } </code></pre> <p>But should it behave more like:</p> <pre><code>func main() { mixer := mylib.NewMixer(&#34;ingredients&#34;) mixed := mixer.Mix(&#34;data&#34;) unmixed := mixer.Unmix(mixed) } </code></pre> <p>or:</p> <pre><code>func main() { mixer := &amp;mylib.NewMixer{Ingredients: &#34;ingredients&#34;} mixed := mixer.Mix(&#34;data&#34;) unmixed := mixer.Unmix(mixed) } </code></pre> <p>Or something else? Or does it matter?</p> <p>Sorry if the question is unclear or stupid. </p> <hr/>**评论:**<br/><br/>daveddev: <pre><p>A &#34;new&#34; function keeps the surface of the API clean, and provides a place for validation of any configuration/dependencies.</p> <p>Regarding using methods or package level functions, the semantics of the second example seem more correct. Package level functions tend to be utilities/helper functions that operate on common types while methods are the force behind a type&#39;s structure (unless the type is acting more like a basic storage type).</p> <p>I&#39;m sure this could could have been explained better, but I hope it helps.</p></pre>JokerSp3: <pre><p>If your mixer requires any work to construct then the New pattern is your best bet. If your struct has defaults and can handle being constructed without any other work (or has no private fields) then keeping it simple (mixer := mylib.Mixer{}) is really nice.</p> <p>The decision to make Mix a method or a top level function depends on how it is used. If it is a function working off a mixer and requires the mixer state then make it a method. If you will ever have a different kind of mixer or you don&#39;t require the mixer make it top level function.</p></pre>driusan: <pre><p>I&#39;m not really sure what you&#39;re trying to do (especially since all of the examples would be compile errors due to unused variables..)</p> <p>That said, 2 looks the best to me. 3 would be okay, except I don&#39;t think your struct type should have &#34;New&#34; in the name.</p> <p>And for all of them, why not just &#34;mixer.Unmix()&#34;?</p></pre>daveddev: <pre><blockquote> <p>And for all of them, why not just &#34;mixer.Unmix()&#34;?</p> </blockquote> <p>mixer.Unmix() (no args) likely does not hold the required dependencies, at least not obviously. This emphasizes the nature of the question for me. It&#39;s not as general of a question as I first took it to be; It seems to be about how OO the API should be, if at all. In that line of thinking another possibility is to call mixed.Unmix()</p> <p>... My current thought is that any answer to this question, as it is now, would be assuming too much about the rest of the package (it&#39;s intent, scope, etc.). Flesh it out and use it. See what&#39;s comfortable as it comes to light.</p></pre>driusan: <pre><p>Oh, you&#39;re right.. I misread that as the same variable name passed multiple times. If anything, it should be mixed.Unmix()...</p> <p>Side note: this is why you should try and make your variable names vary by more than 1 character..</p></pre>

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

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