What's the nicest way to set the maximum value of integer flag?

blov · · 316 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have program with several flags and I would like to somehow limit their value(not all of them). Now, what&#39;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&#39;s a configuration error.</p></pre>kerakk19: <pre><p>I&#39;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&#39;s the typical helper function I write for that.</p> <pre><code>func intInRange(i, min, max int) int { switch { case i &lt; min: return min case i &gt; 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

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