Why is int used rather than int64 in this piece of code?

xuanbao · · 470 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am a Go beginner and trying to understand more about the language by studying code out in the wild. I have been studying this snippet:</p> <p><a href="https://github.com/hailocab/go-geoindex/blob/master/queue.go">https://github.com/hailocab/go-geoindex/blob/master/queue.go</a></p> <p>Overall it makes sense.</p> <p>But I do not understand why in the queue struct the size and cap names are int and not int64, especially as these are cast to int64 throughout the code?</p> <p>The other part I do not understand is the usage of something%int64(queue.cap)... I understand that means modulo the cap of the queue, presumably to handle the case something tries to access a queue slot that is beyond the queue&#39;s capacity but wouldn&#39;t it make more sense just to check if something&gt;queue.cap and error out rather than assign a random queue element modulo the queue.cap?</p> <hr/>**评论:**<br/><br/>TheMerovius: <pre><p>The int64 make no sense. The int <em>does</em> make sense (as that&#39;s the type returned by len() and cap() for slices), but mixing int64 and ints doesn&#39;t. So they should just stick with int.</p> <p>To anser the second part: Because it&#39;s a circular queue (a popular way to implement a double ended queue). So, you might fill the queue completely (until the underlying slice is filled) and then pop from the front. When you then push, they want to reuse the free space at the start. So they wrap around the index.</p> <p>Overall, I&#39;d say: Don&#39;t fall into the trap of assuming that every go code is <em>good</em> go code. This code in particular doesn&#39;t look very good to me, it looks more complicated then it needs to be (for example, they shouldn&#39;t really need len or cap) and has quite a bunch of weirdness in it (the int vs. int64 is only the start).</p></pre>blackcoinprophet: <pre><p>Thanks so much for the helpful reply.</p> <p>What would be some good Go code to study on github? Ideally not something <em>too</em> big.</p></pre>TheMerovius: <pre><p>I&#39;m not necessarily saying &#34;don&#39;t study it&#34;, just &#34;don&#39;t assume everything they do is good&#34;. Just read code by different people and keep an open mind and feel free to question peoples approach (as you did here, IMO correctly) :)</p></pre>birdsaresodumb: <pre><p>The casts are necessary in certain cases.</p> <pre><code>queue.end = int64(queue.size) </code></pre> <p>queue.end could be a number higher than an int&#39;s capacity, for example.</p></pre>joushou: <pre><p>True, but there&#39;s not much reason to use an int64 for the size. You won&#39;t be able to allocate a slice larger than int32 on a platform where int is 32 bits anyway.</p></pre>carrier_pigeon: <pre><p>Without looking at the code, on mobile. Compilation makes the &#39;default&#39; int to either int64 or int 32 depending on arch. I&#39;m still lying in bed and may come back to it later on today if nobody else does. But I hope that bit of info helps. </p></pre>

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

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