diff --git a/example/cast/Cast_test.go b/example/cast/CastBasic_test.go similarity index 96% rename from example/cast/Cast_test.go rename to example/cast/CastBasic_test.go index d194e7f..62c348b 100644 --- a/example/cast/Cast_test.go +++ b/example/cast/CastBasic_test.go @@ -12,7 +12,7 @@ import ( "testing" ) -func TestCast(t *testing.T) { +func TestCastBasic(t *testing.T) { // ToString fmt.Println(cast.ToString("leedarjun")) // leedarjun fmt.Println(cast.ToString(8)) // 8 diff --git a/example/cast/CastMap_test.go b/example/cast/CastMap_test.go new file mode 100644 index 0000000..10cf75c --- /dev/null +++ b/example/cast/CastMap_test.go @@ -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] +} diff --git a/example/cast/CastPoint_test.go b/example/cast/CastPoint_test.go new file mode 100644 index 0000000..aedfc02 --- /dev/null +++ b/example/cast/CastPoint_test.go @@ -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 +} diff --git a/example/cast/CastSlice_test.go b/example/cast/CastSlice_test.go new file mode 100644 index 0000000..c53cfdc --- /dev/null +++ b/example/cast/CastSlice_test.go @@ -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] +} diff --git a/example/cast/CastTimeDuration_test.go b/example/cast/CastTimeDuration_test.go new file mode 100644 index 0000000..b41c525 --- /dev/null +++ b/example/cast/CastTimeDuration_test.go @@ -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 +} diff --git a/example/cast/readme.md b/example/cast/readme.md index c5a0038..f1bb134 100644 --- a/example/cast/readme.md +++ b/example/cast/readme.md @@ -2,4 +2,6 @@ To..方法直接返回转换后的类型 -To..E方法,返回转换后的类型和报错信息 \ No newline at end of file +To..E方法,返回转换后的类型和报错信息 + +参考 go每日一库:https://github.com/darjun/go-daily-lib \ No newline at end of file