Key point: The bottom layer of string is a byte array, so string can also be sliced.
First let’s take a look at a demo
str := "121"
fmt.Println(str[0]) //string底层是一个byte数组,因此string也可以进行切片处理
Output results
49
explain:
1. First, let’s take a look at the data type after string slicing.
fmt.Printf("type=%T",str[0])
type=uint8
fmt.Printf("type=%T",str[:1])
type=string
2.When the byte array is output, the corresponding ASCII will be output.
decimal | graphics |
49 | 1 |
(1) str[0] is converted into the corresponding int
1.strInt := int(str[0])
(2) str[:1] is converted into the corresponding int
2.strInt := int([]byte(str[:1]))