Parse Numerals from a String in Golang
In your question, you wrote:

func CharToNum(r rune) (int, error) {
    for i := 48; i &lt;= 57; i++ {
        if int(r) == i {
            return (int(r) - 48), nil
        }
    }
    return -1, errors.New("type: rune was not int")
}


In his answer, Janos wrote:

func CharToNum(r rune) (int, error) {
    intval := int(r) - '0'
    if 0 &lt;= intval &amp;&amp; intval &lt;= 9 {
        return intval, nil
    }
    return -1, errors.New("type: rune was not int")
}


Using idiomatic Go, I wrote,

var ErrRuneNotInt = errors.New("type: rune was not int")

func CharToNum(r rune) (int, error) {
    if '0' &lt;= r &amp;&amp; r &lt;= '9' {
        return int(r) - '0', nil
    }
    return 0, ErrRuneNotInt
}


It's simple and direct and it's fast. Benchmarking the numbers zero through nine:

BenchmarkCharToNumTschfld    5000000    323 ns/op
BenchmarkCharToNumPeter     50000000     37.7 ns/op


Benchmarking the invalid numeric character space (' '):

BenchmarkCharToNumErrTschfld    10000000      242 ns/op       16 B/op      1 allocs/op
BenchmarkCharToNumErrPeter      2000000000      1.26 ns/op     0 B/op      0 allocs/op


The error variable ErrRuneNotInt can be used to check for a specific error, for example, if err == ErrRuneNotInt {}.



In your question, you wrote:

func ParseNum(str string) []int {
    var nums []int
    for _, val := range str {
        num, err := CharToNum(val)
        if err != nil {
            continue
        }
        nums = append(nums, num)
    }
    return nums
}


Using idiomatic Go, I wrote,

func ParseNum(s string) []int {
    nLen := 0
    for i := 0; i &lt; len(s); i++ {
        if b := s[i]; '0' &lt;= b &amp;&amp; b &lt;= '9' {
            nLen++
        }
    }
    var n = make([]int, 0, nLen)
    for i := 0; i &lt; len(s); i++ {
        if b := s[i]; '0' &lt;= b &amp;&amp; b &lt;= '9' {
            n = append(n, int(b)-'0')
        }
    }
    return n
}


It minimizes both CPU time and memory allocations; it's fast. Benchmarking the string "000123456789000":

BenchmarkParseNumTschfld     1000000    2475 ns/op     248 B/op    5 allocs/op
BenchmarkParseNumPeter       2000000     610 ns/op     128 B/op    1 allocs/op


Unicode and UTF-8 characters corresponding to the ASCII set have the same byte values as ASCII.



See Benchmarks, Package testing.