GO数值和字符串的相互转换,借助strconv包的函数来转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
"fmt"
"strconv"
)

func main() {
myAgeString := "26"
fmt.Printf("My Age: %s\n", myAgeString)

ageValue1, err := strconv.Atoi(myAgeString)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Age: %d\n", ageValue1)

ageValue2 := strconv.Itoa(ageValue1)
fmt.Printf("New Age: %s\n",ageValue2)
}