int64 vs int | Adding in GoLang

xuanbao · · 447 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>So I am sorry if this is the wrong subreddit to be posting this but I ran into an unusual error today that I wasn&#39;t able to come up with a solution for... I have a string being parsed to an int via strconv.ParseInt which returns an int64, but once I have the int64 I cannot seem to add other numbers to it. Is there something I am missing? </p> <pre><code>stringVal := &#34;1&#34; valInt1, errValInt1 := strconv.ParseInt(stringVal, 0 , 64) currentVal := valInt1 newVal := currentVal + 1 </code></pre> <p>Thank you for any advice or help. </p> <p>Edit: Thanks everyone... outside of this I was casting it back to string to place it in a cache... looks like you can&#39;t use string(int64) for int64s but rather need to use strconv.FormatInt()</p> <hr/>**评论:**<br/><br/>JHunz: <pre><p>What exactly is the problem? I ran that on the playground and newVal evaluated to 2, as I would expect. </p></pre>CenlTheFenl: <pre><p>I put the fix in the post, turns out to be an issue with casting it back to a string. </p> <p>Thanks! </p></pre>shovelpost: <pre><p>Seems like <a href="https://play.golang.org/p/R2Z4mbbXHp6" rel="nofollow">it is working</a>. What is the problem?</p> <p>P.S. Don&#39;t forget to check your errors.</p></pre>CenlTheFenl: <pre><p>I put the fix in the post, turns out to be an issue with casting it back to a string. </p> <p>Thanks! </p></pre>TheMerovius: <pre><p>Go does not have automatic conversions, even between numeric types. The reason is, that the subtleties around (integer) conversions in C are a common source of bugs, are often hard to understand and sometimes lead to security problems (because of over- or underflows). So you have to explicitly convert, if you want to operate between different types:</p> <pre><code>x := int(23) y := int64(42) fmt.Println(int64(x) + y) </code></pre> <p>It forces you to think whether a conversion is actually <em>safe</em>, or whether there might be overflow; personally, I find that a very useful feature :)</p></pre>CenlTheFenl: <pre><p>Yeah, GO is new to me... I started all of this about a week ago and just have been playing with GO + MUX + HTTP. I never did program in C or C++ so some of the unassumed things are new to me. </p></pre>TheMerovius: <pre><p>Well, welcome :) It&#39;s fine to be new, I hope you keep at it and keep asking questions :)</p> <p>(and FYI, there is a #golang-newbies channel on the <a href="https://invite.slack.golangbridge.org/" rel="nofollow">gopher slack</a> specifically dedicated to asking beginner-questions; it might be interesting to join that).</p></pre>porkbonk: <pre><blockquote> <p>Edit: Thanks everyone... outside of this I was casting it back to string to place it in a cache... looks like you can&#39;t use string(int64) for int64s but rather need to use strconv.FormatInt()</p> </blockquote> <pre><code>num := int64(61) fmt.Println(num, string(num)) </code></pre> <p>prints <code>61 =</code></p> <p>The int64 gets interpreted as a rune. Remember that you&#39;re <em>casting</em> between types which is different from parsing and formatting. </p> <p>As you figured out <code>strconv.FormatInt</code> is what you want to use. Also make sure to get familiar with the <a href="https://godoc.org/fmt" rel="nofollow">fmt package</a>. The printf functions allow a lot of control over the formatting for any type including integers.</p></pre>kostix: <pre><p>What is the problem statement?</p> <blockquote> <p>I cannot seem to add other numbers to it</p> </blockquote> <p>does not count as one.</p></pre>CenlTheFenl: <pre><p>The issue was it didn&#39;t seem like the number was incrementing but rather giving a formatting error. What I didn&#39;t check was the conversion back to string. </p></pre>joncalhoun: <pre><p>I&#39;m not sure what you are doing, but you may want to check out the <a href="https://golang.org/pkg/strconv/#Atoi" rel="nofollow">Atoi</a> function which assumes base 10 and is typically what people want when parsing integers from strings.</p></pre>

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

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