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:
CenlTheFenl:What exactly is the problem? I ran that on the playground and newVal evaluated to 2, as I would expect.
shovelpost:I put the fix in the post, turns out to be an issue with casting it back to a string.
Thanks!
CenlTheFenl:Seems like it is working. What is the problem?
P.S. Don't forget to check your errors.
TheMerovius:I put the fix in the post, turns out to be an issue with casting it back to a string.
Thanks!
CenlTheFenl: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 :)
TheMerovius: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.
porkbonk: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).
kostix: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.
CenlTheFenl:What is the problem statement?
I cannot seem to add other numbers to it
does not count as one.
joncalhoun: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.
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.
