int64 vs int | Adding in GoLang

xuanbao · 2018-03-10 08:30:10 · 569 次点击    
这是一个分享于 2018-03-10 08:30:10 的资源,其中的信息可能已经有所发展或是发生改变。

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'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?

stringVal := "1" 
valInt1, errValInt1 := strconv.ParseInt(stringVal, 0 , 64)
currentVal := valInt1
newVal := currentVal + 1

Thank you for any advice or help.

Edit: Thanks everyone... outside of this I was casting it back to string to place it in a cache... looks like you can't use string(int64) for int64s but rather need to use strconv.FormatInt()


评论:

JHunz:

What exactly is the problem? I ran that on the playground and newVal evaluated to 2, as I would expect.

CenlTheFenl:

I put the fix in the post, turns out to be an issue with casting it back to a string.

Thanks!

shovelpost:

Seems like it is working. What is the problem?

P.S. Don't forget to check your errors.

CenlTheFenl:

I put the fix in the post, turns out to be an issue with casting it back to a string.

Thanks!

TheMerovius:

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:

x := int(23)
y := int64(42)
fmt.Println(int64(x) + y)

It forces you to think whether a conversion is actually safe, or whether there might be overflow; personally, I find that a very useful feature :)

CenlTheFenl:

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.

TheMerovius:

Well, welcome :) It's fine to be new, I hope you keep at it and keep asking questions :)

(and FYI, there is a #golang-newbies channel on the gopher slack specifically dedicated to asking beginner-questions; it might be interesting to join that).

porkbonk:

Edit: Thanks everyone... outside of this I was casting it back to string to place it in a cache... looks like you can't use string(int64) for int64s but rather need to use strconv.FormatInt()

num := int64(61)
fmt.Println(num, string(num))

prints 61 =

The int64 gets interpreted as a rune. Remember that you're casting between types which is different from parsing and formatting.

As you figured out strconv.FormatInt is what you want to use. Also make sure to get familiar with the fmt package. The printf functions allow a lot of control over the formatting for any type including integers.

kostix:

What is the problem statement?

I cannot seem to add other numbers to it

does not count as one.

CenlTheFenl:

The issue was it didn't seem like the number was incrementing but rather giving a formatting error. What I didn't check was the conversion back to string.

joncalhoun:

I'm not sure what you are doing, but you may want to check out the Atoi function which assumes base 10 and is typically what people want when parsing integers from strings.


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

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