Go 语言类型转换的陷阱

1

介绍

Go 语言作为强类型语言,在使用 Golang 开发项目时,经常会遇到类型转换的场景,整型之间可以直接转换,字节切片和字符串之间也可以直接转换。

但是,如果整型和字符串之间做类型转换,则需要使用 strconv 标准库提供的函数。


2

标准库 strconv 类型转换

Go 语言标准库 strconv 提供了一些类型转换的函数,比如在项目开发中使用比较多的整型和字符串之间的类型转换。

Go 复制代码
func main() {
 salary := 5000
 salaryStr := strconv.Itoa(salary)
 fmt.Printf("%T salary=%d\n", salary, salary)
 fmt.Printf("%T salaryStr=%s\n", salaryStr, salaryStr)

 age := "23"
 ageInt, err := strconv.Atoi(age)
 fmt.Printf("%T age=%s\n", age, age)
 fmt.Printf("%T ageInt=%d err=%v\n", ageInt, ageInt, err)
}

输出结果:

bash 复制代码
int salary=5000
string salaryStr=5000
string age=23
int ageInt=23 err=<nil>

阅读上面这段代码,我们使用标准库 strconv 将整型变量 salary 转换为字符串类型变量 salaryStr;将字符串类型变量 age 转换为整型变量 ageInt。

但是,读者朋友们有没有发现一个问题,我们使用标准库 strconv 提供的函数 Atoi 将字符串类型变量转换为整型变量,得到的是 int 类型,如果我们需要得到一个 int8 类型的变量,我们需要继续做类型转换,例如:

Go 复制代码
age := "23"
ageInt, err := strconv.Atoi(age)
ageInt8 := int8(ageInt)

也就是说,如果我们需要将一个字符串类型的变量转换为一个非 int 类型的整型变量,需要做二次转换,在实际项目开发中,使用起来稍微繁琐一些。

此外,使用标准库 strconv 做类型转换,除了在一些场景中稍微繁琐之外,还有另外一个问题,我们先阅读以下一段代码。

Go 复制代码
func main() {
  phoneNumber := "138001380001380013800013800138000"
 phoneNumberInt, err := strconv.Atoi(phoneNumber)
 fmt.Printf("%T phoneNumber=%s\n", phoneNumber, phoneNumber)
 fmt.Printf("%T phoneNumberInt=%d err=%v\n", phoneNumberInt, phoneNumberInt, err)
}

输出结果:

bash 复制代码
string phoneNumber=138001380001380013800013800138000
int phoneNumberInt=9223372036854775807 err=strconv.Atoi: parsing "138001380001380013800013800138000": value out of range

阅读上面这段代码输出的错误信息 value out of range,也就是说如果我们需要转换的值超出返回,Go 语言标准库 strconv 提供的函数 Atoi 会返回错误。

所以,在使用函数 Atoi 时,我们要做好参数验证和错误处理。

有没有使用更简单的类型转换库,接下来,我们来看一下流行的三方库 cast。


3

三方库 cast 类型转换

Go 类型转换的三方库 cast 是一个使用比较多的库,我们使用 cast 来处理 Part02 的类型转换需求,代码如下:

Go 复制代码
func main() {
  age2 := "23"
 age2Int8 := cast.ToInt8(age2)
 fmt.Printf("%T age2=%s\n", age2, age2)
 fmt.Printf("%T age2Int8=%d\n", age2Int8, age2Int8)

 phoneNumber2 := "138001380001380013800013800138000"
 phoneNumber2Int := cast.ToInt(phoneNumber2)
 fmt.Printf("%T phoneNumber2=%s\n", phoneNumber2, phoneNumber2)
 fmt.Printf("%T phoneNumber2Int=%d\n", phoneNumber2Int, phoneNumber2Int)
}

输出结果:

复制代码
string age2=23
int8 age2Int8=23
string phoneNumber2=138001380001380013800013800138000
int phoneNumber2Int=0

阅读上面这段代码,我们可以发现,使用 cast 可以直接将字符串类型的变量转换为我们需要的整型变量,使用起来不再感到繁琐。

同时,需要注意的是,如果转换失败,将返回类型零值,字符串类型变量 phoneNumber2 在使用 cast 转换为 int 类型的变量时,返回的结果就是 int 的类型零值。

使用 cast 比使用 strconv 更简单,而且不需要处理错误。但是,cast 还有一个陷阱,我们需要特别注意一下,我们先阅读以下一段代码:

Go 复制代码
func main() {
  month := "07"
 monthInt8 := cast.ToInt8(month)
 fmt.Printf("%T month=%s\n", month, month)
 fmt.Printf("%T monthInt8=%d\n", monthInt8, monthInt8)

 month2 := "08"
 month2Int8 := cast.ToInt8(month2)
 fmt.Printf("%T month2=%s\n", month2, month2)
 fmt.Printf("%T month2Int8=%d\n", month2Int8, month2Int8)
}

输出结果:

复制代码
string month=07
int8 monthInt8=7
string month2=08
int8 month2Int8=0

阅读上面这段代码的输出结果,我们可以发现使用 cast 将字符串类型 month 和 month2 转换为整型时,字符串是以 "0" 开头的月份,"07" 转换后得到整型 7,而 "08" 转换后得到整型 0。

我们再使用 strconv 转换 "08",代码如下:

Go 复制代码
func main() {
  month2 := "08"
 month2Int8 := cast.ToInt8(month2)
 fmt.Printf("%T month2=%s\n", month2, month2)
 fmt.Printf("%T month2Int8=%d\n", month2Int8, month2Int8)

 month2Int2, err := strconv.Atoi(month2)
 fmt.Printf("%T month2Int2=%d err=%v\n", month2Int2, month2Int2, err)
}

输出结果:

复制代码
int8 month2Int8=0
int month2Int2=8 err=<nil>

读者朋友们从输出结果可以看到,"08" 使用 strconv 转换后得到整型 8,所以我们在转换以一个或多个 "0" 开头的字符串为整型时,字符串 "0" 后面的数值大于 7 将不能使用 cast 转换,最好就是在转换以一个或多个 "0" 开头的字符串为整型时,比如 "08"、"009"、"00010" 等,使用 strconv 转换,而不要使用 cast 转换。


4

总结

本文我们介绍 Go 语言类型转换的两个库,分别是标准库 strconv 和三方库 cast,其中 cast 更方便、更安全,但是也有陷阱,我们需要特别注意,避免在项目开发中掉进陷阱。

关于这两个类型转换库的更多用法,感兴趣的读者朋友们可以熟读手册,多多动手练习。

参考资料:

strconv: https://pkg.go.dev/strconv

cast: https://github.com/spf13/cast

相关推荐
利威尔xu3 小时前
2026-11-05-Go内存管理面试
面试·golang
Zenova EdgeOS2 天前
Go 工业边缘错误处理实战:用 errors 构建可追溯错误链
开发语言·数据库·golang
JavaPub-rodert2 天前
从零搭建通用管理后台:ShiyuAdmin 的 Go、React 与 RBAC 实践
开发语言·react.js·golang
microrain2 天前
SagooIoT 的数据接入层只有四张表,却能接住三种外部数据
物联网·golang·数据建模·sagooiot
fly-iot2 天前
【FDE知识库】(4):企业知识库怎么选型,开源知识库分享几个,选择开源协议友好的版本,开发wiki-go项目
golang·开源·开源协议
福大大架构师每日一题2 天前
excelize v2.11.1发布:Go 1.26.0起步,16项安全修复与嵌入对象能力全面升级
开发语言·安全·golang
小小龙学IT2 天前
Go 语言 pprof 性能剖析与调优深度解析:把“卡在哪“变成一张可执行的数据图
开发语言·后端·golang
十二同学啊2 天前
Go 工程化详解:项目结构、代码规范、测试体系与 CI/CD 实践
ci/cd·golang·代码规范
利威尔xu3 天前
Go IM 源码实战(14):本地缓存快 3000 倍,失效却难 10 倍
后端·缓存·微服务·面试·golang
利威尔xu3 天前
Go IM 源码实战(13):30 个会话只有 2 个红点,未读数错在三条路径
redis·后端·微服务·面试·golang