高权重网站出售,oppo手机商城,网站做反向代理后样式加载错误,seo指的是什么意思1
介绍 Go 语言作为强类型语言#xff0c;在使用 Golang 开发项目时#xff0c;经常会遇到类型转换的场景#xff0c;整型之间可以直接转换#xff0c;字节切片和字符串之间也可以直接转换。
但是#xff0c;如果整型和字符串之间做类型转换#xff0c;则需要使用 str…1
介绍 Go 语言作为强类型语言在使用 Golang 开发项目时经常会遇到类型转换的场景整型之间可以直接转换字节切片和字符串之间也可以直接转换。
但是如果整型和字符串之间做类型转换则需要使用 strconv 标准库提供的函数。 2
标准库 strconv 类型转换
Go 语言标准库 strconv 提供了一些类型转换的函数比如在项目开发中使用比较多的整型和字符串之间的类型转换。
func main() {salary : 5000salaryStr : strconv.Itoa(salary)fmt.Printf(%T salary%d\n, salary, salary)fmt.Printf(%T salaryStr%s\n, salaryStr, salaryStr)age : 23ageInt, err : strconv.Atoi(age)fmt.Printf(%T age%s\n, age, age)fmt.Printf(%T ageInt%d err%v\n, ageInt, ageInt, err)
}
输出结果
int salary5000
string salaryStr5000
string age23
int ageInt23 errnil
阅读上面这段代码我们使用标准库 strconv 将整型变量 salary 转换为字符串类型变量 salaryStr将字符串类型变量 age 转换为整型变量 ageInt。
但是读者朋友们有没有发现一个问题我们使用标准库 strconv 提供的函数 Atoi 将字符串类型变量转换为整型变量得到的是 int 类型如果我们需要得到一个 int8 类型的变量我们需要继续做类型转换例如
age : 23
ageInt, err : strconv.Atoi(age)
ageInt8 : int8(ageInt)
也就是说如果我们需要将一个字符串类型的变量转换为一个非 int 类型的整型变量需要做二次转换在实际项目开发中使用起来稍微繁琐一些。
此外使用标准库 strconv 做类型转换除了在一些场景中稍微繁琐之外还有另外一个问题我们先阅读以下一段代码。
func main() {phoneNumber : 138001380001380013800013800138000phoneNumberInt, err : strconv.Atoi(phoneNumber)fmt.Printf(%T phoneNumber%s\n, phoneNumber, phoneNumber)fmt.Printf(%T phoneNumberInt%d err%v\n, phoneNumberInt, phoneNumberInt, err)
}
输出结果
string phoneNumber138001380001380013800013800138000
int phoneNumberInt9223372036854775807 errstrconv.Atoi: parsing 138001380001380013800013800138000: value out of range阅读上面这段代码输出的错误信息 value out of range也就是说如果我们需要转换的值超出返回Go 语言标准库 strconv 提供的函数 Atoi 会返回错误。
所以在使用函数 Atoi 时我们要做好参数验证和错误处理。
有没有使用更简单的类型转换库接下来我们来看一下流行的三方库 cast。 3
三方库 cast 类型转换
Go 类型转换的三方库 cast 是一个使用比较多的库我们使用 cast 来处理 Part02 的类型转换需求代码如下
func main() {age2 : 23age2Int8 : cast.ToInt8(age2)fmt.Printf(%T age2%s\n, age2, age2)fmt.Printf(%T age2Int8%d\n, age2Int8, age2Int8)phoneNumber2 : 138001380001380013800013800138000phoneNumber2Int : cast.ToInt(phoneNumber2)fmt.Printf(%T phoneNumber2%s\n, phoneNumber2, phoneNumber2)fmt.Printf(%T phoneNumber2Int%d\n, phoneNumber2Int, phoneNumber2Int)
}
输出结果
string age223
int8 age2Int823
string phoneNumber2138001380001380013800013800138000
int phoneNumber2Int0
阅读上面这段代码我们可以发现使用 cast 可以直接将字符串类型的变量转换为我们需要的整型变量使用起来不再感到繁琐。
同时需要注意的是如果转换失败将返回类型零值字符串类型变量 phoneNumber2 在使用 cast 转换为 int 类型的变量时返回的结果就是 int 的类型零值。
使用 cast 比使用 strconv 更简单而且不需要处理错误。但是cast 还有一个陷阱我们需要特别注意一下我们先阅读以下一段代码
func main() {month : 07monthInt8 : cast.ToInt8(month)fmt.Printf(%T month%s\n, month, month)fmt.Printf(%T monthInt8%d\n, monthInt8, monthInt8)month2 : 08month2Int8 : cast.ToInt8(month2)fmt.Printf(%T month2%s\n, month2, month2)fmt.Printf(%T month2Int8%d\n, month2Int8, month2Int8)
}
输出结果
string month07
int8 monthInt87
string month208
int8 month2Int80
阅读上面这段代码的输出结果我们可以发现使用 cast 将字符串类型 month 和 month2 转换为整型时字符串是以 0 开头的月份07 转换后得到整型 7而 08 转换后得到整型 0。
我们再使用 strconv 转换 08代码如下
func main() {month2 : 08month2Int8 : 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 month2Int80
int month2Int28 errnil
读者朋友们从输出结果可以看到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