16、strconv

1.strconv是什么?

strconv是用来处理字符串和基本类型之间的转换的

2.strconv的使用

/**
 * @author ly (个人博客:https://www.cnblogs.com/qbbit)
 * @date 2023/5/2  14:25
 * @tags 喜欢就去努力的争取
 */
package main

import (
	"fmt"
	"strconv"
)

func main() {
	
	/**
		strconv.FormatXXX : 其他类型 ---> 字符串
		strconv.ParseXXX : 字符串 ---> 其他类型
		itoa() : int类型 ---> 字符串
		atoi() : 字符串 ---> int类型
	 */

	// bool类型
	s1 := "true"
	parseBool, err := strconv.ParseBool(s1)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%T,%t \n", parseBool, parseBool)
	}

	// 整数类型
	s2 := "100"
	i, err := strconv.ParseInt(s2, 10, 64)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%T,%d \n", i, i)
	}
	str := strconv.FormatInt(i, 10)
	fmt.Printf("%T,%s\n", str, str)

	// itoa(),atoi()
	i2, err := strconv.Atoi("-100") // 转int类型
	fmt.Printf("%T,%d\n", i2, i2)

	str2 := strconv.Itoa(i2) // 转为string类型
	fmt.Printf("%T,%s\n", str2, str2)

}

posted @   我也有梦想呀  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2022-05-02 SpringBoot-Validate优雅的实现参数校验,详细示例~
点击右上角即可分享
微信分享提示