feat(Go-StudyExample):增加cast类型转换工具包测试

This commit is contained in:
Huangzj
2020-12-28 09:56:34 +08:00
parent 15ff62152d
commit cdc446fe8a
6 changed files with 134 additions and 2 deletions
@@ -12,7 +12,7 @@ import (
"testing" "testing"
) )
func TestCast(t *testing.T) { func TestCastBasic(t *testing.T) {
// ToString // ToString
fmt.Println(cast.ToString("leedarjun")) // leedarjun fmt.Println(cast.ToString("leedarjun")) // leedarjun
fmt.Println(cast.ToString(8)) // 8 fmt.Println(cast.ToString(8)) // 8
+43
View File
@@ -0,0 +1,43 @@
/*
* @Author : huangzj
* @Time : 2020/12/28 9:51
* @Description
*/
package cast
import (
"fmt"
"github.com/spf13/cast"
"testing"
)
func TestCastMap(t *testing.T) {
m1 := map[string]string{
"name": "darjun",
"job": "developer",
}
m2 := map[string]interface{}{
"name": "jingwen",
"age": 18,
}
m3 := map[interface{}]string{
"name": "pipi",
"job": "designer",
}
m4 := map[interface{}]interface{}{
"name": "did",
"age": 29,
}
jsonStr := `{"name":"bibi", "job":"manager"}`
fmt.Println(cast.ToStringMapString(m1)) // map[job:developer name:darjun]
fmt.Println(cast.ToStringMapString(m2)) // map[age:18 name:jingwen]
fmt.Println(cast.ToStringMapString(m3)) // map[job:designer name:pipi]
fmt.Println(cast.ToStringMapString(m4)) // map[job:designer name:pipi]
fmt.Println(cast.ToStringMapString(jsonStr)) // map[job:manager name:bibi]
}
+22
View File
@@ -0,0 +1,22 @@
/*
* @Author : huangzj
* @Time : 2020/12/28 9:52
* @Description
*/
package cast
import (
"fmt"
"github.com/spf13/cast"
"testing"
)
func TestCastPoint(t *testing.T) {
p := new(int)
*p = 8
fmt.Println(cast.ToInt(p)) // 8
pp := &p
fmt.Println(cast.ToInt(pp)) // 8
}
+31
View File
@@ -0,0 +1,31 @@
/*
* @Author : huangzj
* @Time : 2020/12/28 9:52
* @Description
*/
package cast
import (
"fmt"
"github.com/spf13/cast"
"testing"
)
func TestSlice(t *testing.T) {
sliceOfInt := []int{1, 3, 7}
arrayOfInt := [3]int{8, 12}
// ToIntSlice
fmt.Println(cast.ToIntSlice(sliceOfInt)) // [1 3 7]
fmt.Println(cast.ToIntSlice(arrayOfInt)) // [8 12 0]
sliceOfInterface := []interface{}{1, 2.0, "darjun"}
sliceOfString := []string{"abc", "dj", "pipi"}
stringFields := " abc def hij "
any := interface{}(37)
// ToStringSliceE
fmt.Println(cast.ToStringSlice(sliceOfInterface)) // [1 2 darjun]
fmt.Println(cast.ToStringSlice(sliceOfString)) // [abc dj pipi]
fmt.Println(cast.ToStringSlice(stringFields)) // [abc def hij]
fmt.Println(cast.ToStringSlice(any)) // [37]
}
+34
View File
@@ -0,0 +1,34 @@
/*
* @Author : huangzj
* @Time : 2020/12/28 9:52
* @Description
*/
package cast
import (
"fmt"
"github.com/spf13/cast"
"testing"
"time"
)
func TestTimeDuration(t *testing.T) {
now := time.Now()
timestamp := 1579615973
timeStr := "2020-01-21 22:13:48"
fmt.Println(cast.ToTime(now)) // 2020-01-22 06:31:50.5068465 +0800 CST m=+0.000997701(时间是当前时间,格式如此)
fmt.Println(cast.ToTime(timestamp)) // 2020-01-21 22:12:53 +0800 CST
fmt.Println(cast.ToTime(timeStr)) // 2020-01-21 22:13:48 +0000 UTC
d, _ := time.ParseDuration("1m30s")
ns := 30000
strWithUnit := "130s"
strWithoutUnit := "130"
fmt.Println(cast.ToDuration(d)) // 1m30s
fmt.Println(cast.ToDuration(ns)) // 30µs
fmt.Println(cast.ToDuration(strWithUnit)) // 2m10s
fmt.Println(cast.ToDuration(strWithoutUnit)) // 130ns
}
+2
View File
@@ -3,3 +3,5 @@
To..方法直接返回转换后的类型 To..方法直接返回转换后的类型
To..E方法,返回转换后的类型和报错信息 To..E方法,返回转换后的类型和报错信息
参考 go每日一库:https://github.com/darjun/go-daily-lib