如何在golang中将字节的ASCII值添加到整数?

I'd like to get a byte[] variables' asic value added to an integer. For example, I fisrt read all input in a byte[] buffer. And then I get out a number string "123" in it. And then I can assign it to an integer by ('1' - '0')*100 + ('2' - '0')*10 + '3' - '0'. But I can not assign integers with byte variables. How can I do that with any means? Thank you very much :)

In go, you can convert byte array to string with the string() coercion and then use strconv.Atoi on it. Presumably, you also want to use slice operations to isolate just the part of the input you want to convert.

package main

import (
    "strconv"
    "fmt"
)

func main() {
    data := []byte { 0x20, 0x31, 0x32, 0x33, 0x20 } // Embedded number
    // string(...) coercion yields a string from a byte buffer
    // Number starts at char 1, ends before char 4
    str := string(data[1:4])
    i, err := strconv.Atoi(str)
    if err != nil {
        fmt.Printf("Error %v
", err)
        return
    }

    fmt.Printf("value %v
", i)
}

Prints

value 123

And since go has nicely hygenic practicies, errors will be handled too.

If you need to read an integer from a stream of bytes, the fastest way would be just to scan it with fmt. Example:

n := 0
_, err := fmt.Scanf("%d", &n)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("you entered %d
", n)