type check q

blov · · 389 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Curious as to why line 14 fails compilation, whereas 15 is ok ? In the below snippet:</p> <pre><code> 12 var fl float64 13 thou := 1000 14 fl = 1.2*thou 15 fl = 1.2*1000 </code></pre> <p>is it that - with variables one can mix wrong types.. with constants it is not that common.</p> <hr/>**评论:**<br/><br/>Femaref: <pre><p><code>thou</code> will be an int and needs to be converted to a float so you can multiply it. With constants it doesn&#39;t matter, as the correct type is chosen automatically.</p></pre>TheMerovius: <pre><p>See <a href="https://golang.org/ref/spec#Constants" rel="nofollow">here</a> for the relevant part of the spec and <a href="https://blog.golang.org/constants" rel="nofollow">here</a> for an in-depth explanation.</p></pre>nagai: <pre><p>Is there any actual reason the compiler can&#39;t infer float here?</p></pre>earthboundkid: <pre><p>Go doesn’t have implicit conversion of things with types because in C this has been an endless source of bugs. Fortunately, literals and constants don’t have to have types, so it’s not a problem in practice. Change to <code>const thou = 1000</code> and it will work. </p></pre>

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

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