I want to convert a string representing and integer into a slice containing each integer that made up the initial string.
I've tried using the Atoi()
function in strconv
, it it gives "cannot use toCheck[i] (type byte) as type string in argument to strconv.Atoi". I've also tried int() conversion, but it's giving me what I believe are ASCII values - I know that I can just subtract 48, but that seems a little hacky.
package main
import (
"fmt"
)
func main() {
toCheck := "987654321"
var toSum []int
for i := len(toCheck) - 2; i >= 0; i = i-2 {
toSum = append(toSum, int(toCheck[i]))
}
fmt.Println(toSum)
// expected 2 4 6 8
// actual 50 52 54 56
}
Expected output: 2 4 6 Actual: 50 52 54 56