string、int、int64相互转换 - Tue, May 7, 2019
string、int、int64相互转换
import "strconv" //先导入strconv包
// string到int
int, err := strconv.Atoi(string)
// string到int64
int64, err := strconv.ParseInt(string, 10, 64)
// int到string
string := strconv.Itoa(int)
// int64到string
string := strconv.FormatInt(int64,10)
example
package main
import (
"fmt"
"strconv"
)
func main() {
// string到int
intValue, err := strconv.Atoi("1557214948")
if err != nil {
fmt.Println("string to int err:", err)
return
}
fmt.Println("int value:", intValue)
// string到int64
int64Value, err := strconv.ParseInt("1557214948", 10, 64)
if err != nil {
fmt.Println("string to int64 err:", err)
return
}
fmt.Println("int64 value:", int64Value)
// int到string
stringValue := strconv.Itoa(intValue)
fmt.Println("int to string value:", stringValue)
// int64到string
stringValue = strconv.FormatInt(int64Value, 10)
fmt.Println("int64 to string value:", stringValue)
}
int value: 1557214948
int64 value: 1557214948
int to string value: 1557214948
int64 to string value: 1557214948