Go 字符串转给基础数据类型

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var a1 = "20"
	var a2 = "true"
	var a3 = "19.862"

	n1, _ := strconv.Atoi(a1)             //字符串转为整型,_ 位标识符,返回的结果有2个,忽略掉第二个,第二个是用于接收error的错误信息
	n6, _ := strconv.ParseInt(a1, 10, 64) //另一种字符串转给整型

	n2, _ := strconv.ParseBool(a2)      //字符串转布尔型
	n3, _ := strconv.ParseFloat(a3, 64) // 字符串转浮点数
	fmt.Printf("n1的类型是%T,对应的值是%#v\n", n1, n1)
	fmt.Printf("n6的类型是%T,对应的值是%#v\n", n6, n6)
	fmt.Printf("n2的类型是%T,对应的值是%#v\n", n2, n2)
	fmt.Printf("n3的类型是%T,对应的值是%#v\n", n3, n3)
}

image

posted @ 2023-01-03 20:12  弩哥++  阅读(23)  评论(0编辑  收藏  举报