<p>I have program with several flags and I would like to somehow limit their value(not all of them). Now, what's the nicest way to do so? Is the function with many ifs only way? </p>
<hr/>**评论:**<br/><br/>xiegeo: <pre><p>Say the limits in the description, which gets printed by -help. Then in your program, check for them and error out if any flag is out of the range. Do not convert them to the acceptable values since it's a configuration error.</p></pre>kerakk19: <pre><p>I'll probably stick with that. It would be cool tho to have limiting option. </p></pre>Zirual: <pre><p>As in you want to limit an int to stay within a range? Here's the typical helper function I write for that.</p>
<pre><code>func intInRange(i, min, max int) int {
switch {
case i < min:
return min
case i > max:
return max
default:
return i
}
}
</code></pre></pre>hell_0n_wheel: <pre><p>Why? That sounds like a bad idea.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传