<p>There is an existing API:
Dict() Event {...}</p>
<p>I want to change it to take a parameter:
Dict(bool compact) Event {...}</p>
<p>I want to do this without impacting callers of prior apis. say old callers will NOT get compact - unless they change the API.</p>
<p>In C++ there is a default value - where i could say Dict (bool compact = false) {} - there is no such thing in go AFAIK.
How do i achieve this in golang ? Here are the ways i could think of - can u suggest a method pls ?</p>
<ol>
<li><p>new API - CompactDict () {} - but the caller has to deal with two APIs instead of one API with two values for a single param.</p></li>
<li><p><HACK> change API to Dict(compact ...bool).. - this way the API can take in zero parameters (current callers) and one or more parameters. If there are more, will take the first one as the valid one and ignore the rest..</p></li>
</ol>
<p>Your comments are appreciated..</p>
<hr/>**评论:**<br/><br/>Sythe2o0: <pre><p>The ways you suggest are the easier ways to do it. </p>
<p>In the future, a way to do this would be to define the functions you want to be able to add more parameters to as taking in a variadic set of <a href="https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis" rel="nofollow">functional options</a>. This involves more boilerplate on your part.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传