类型转换
在Golang开发中,类型转换是个不可避免的过程,我们经常需要把字符串转成int、bool类型,使用原生提供的strconv标准包比较繁琐,还需要处理err异常:
func ToInt(a string) int {
b, _ := strconv.Atoi(a)
return b
}
func ToBool(a string) bool {
if a == "true" || a == "TRUE" {
return true
} else if a == "false" || a == "FALSE" {
return false
}
return a == "1"
}
Cast库
介绍
Cast库的作用就是封装了很多类型转换的方法,让引用这个库的开发者,能够以极简的方式把任意类型的值转换成指定类型。
package test
import (
"encoding/json"
"fmt"
"github.com/spf13/cast"
"go-learning/dao"

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



