feat(Go-StudyExample):2020/12/28:新增cast类型转换工具包
This commit is contained in:
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2020/12/28 9:18
|
||||||
|
* @Description:测试cast,这个包是把interface,根据类型进行转换的处理
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cast
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/spf13/cast"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCast(t *testing.T) {
|
||||||
|
// ToString
|
||||||
|
fmt.Println(cast.ToString("leedarjun")) // leedarjun
|
||||||
|
fmt.Println(cast.ToString(8)) // 8
|
||||||
|
fmt.Println(cast.ToString(8.31)) // 8.31
|
||||||
|
fmt.Println(cast.ToString([]byte("one time"))) // one time
|
||||||
|
fmt.Println(cast.ToString(nil)) // ""
|
||||||
|
|
||||||
|
var foo interface{} = "one more time"
|
||||||
|
fmt.Println(cast.ToString(foo)) // one more time
|
||||||
|
|
||||||
|
// ToInt
|
||||||
|
fmt.Println(cast.ToInt(8)) // 8
|
||||||
|
fmt.Println(cast.ToInt(8.31)) // 8
|
||||||
|
fmt.Println(cast.ToInt("8")) // 8
|
||||||
|
fmt.Println(cast.ToInt(true)) // 1
|
||||||
|
fmt.Println(cast.ToInt(false)) // 0
|
||||||
|
|
||||||
|
var eight interface{} = 8
|
||||||
|
fmt.Println(cast.ToInt(eight)) // 8
|
||||||
|
fmt.Println(cast.ToInt(nil)) // 0
|
||||||
|
//To..E方法会返回转换错误的报错信息
|
||||||
|
_, e := cast.ToIntE("asda")
|
||||||
|
if e != nil {
|
||||||
|
fmt.Println(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
获取包命令:go get github.com/spf13/cast
|
||||||
|
|
||||||
|
To..方法直接返回转换后的类型
|
||||||
|
|
||||||
|
To..E方法,返回转换后的类型和报错信息
|
||||||
@@ -31,7 +31,9 @@ json与struct之间转换处理工具使用示例
|
|||||||
|
|
||||||
2020/12/25:新增leetcode部分习题(数据结构、二进制相关)
|
2020/12/25:新增leetcode部分习题(数据结构、二进制相关)
|
||||||
|
|
||||||
2020/12/25:新增carbon时间操作库使用示例
|
2020/12/25: 新增carbon时间操作库使用示例
|
||||||
|
|
||||||
|
2020/12/28:新增cast类型转换工具包
|
||||||
|
|
||||||
# mod vendor模式加载包
|
# mod vendor模式加载包
|
||||||
通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到
|
通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到
|
||||||
|
|||||||
Reference in New Issue
Block a user