###### tags: `Golang Package` # Cast https://segmentfault.com/a/1190000021684109 類型轉換庫,將字串轉數字、數字轉字串、轉成布林值等。 ## ToString ```go= 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)) // "" fmt.Println(cast.ToString(false)) // false ``` ## ToInt ```go= 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 ``` ## ToBool ```go= fmt.Println(cast.ToBool("1")) // true fmt.Println(cast.ToBool(1)) // true fmt.Println(cast.ToBool(nil)) // false fmt.Println(cast.ToBool("2")) // false ``` ## ToSliceBool ```go= fmt.Println(cast.ToBoolSlice([]string{"1", "0"})) // [ture false] ``` ## ToDuration ```go= fmt.Println(cast.ToDuration(1398173719283712328)) // 388381h35m19.283712328s // --------------------------------------------------------------- func ToDurationE(i interface{}) (d time.Duration, err error) { i = indirect(i) switch s := i.(type) { case time.Duration: return s, nil case int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8: d = time.Duration(ToInt64(s)) return case float32, float64: d = time.Duration(ToFloat64(s)) return case string: if strings.ContainsAny(s, "nsuµmh") { d, err = time.ParseDuration(s) } else { d, err = time.ParseDuration(s + "ns") } return default: err = fmt.Errorf("unable to cast %#v of type %T to Duration", i, i) return } } ``` ## ToFloat32 ```go= fmt.Println(cast.ToFloat32("239.1")) // 239.1 fmt.Println(cast.ToFloat32(int64(192)))// 192 fmt.Println(cast.ToFloat32(false)) // 0 ``` ## ToStringMap ```go= fmt.Println(cast.ToStringMap(map[string]interface{}{ "one": "1", "two": 2, "three": float32(3.4), "four": uint8(5), "five": false, })) /* (map[string]interface {}) (len=5) { (string) (len=5) "three": (float32) 3.4, (string) (len=4) "four": (uint8) 5, (string) (len=4) "five": (bool) false, (string) (len=3) "one": (string) (len=1) "1", (string) (len=3) "two": (int) 2 } */ ``` ## ToStringMapString ```go= spew.Dump(cast.ToStringMapString(map[string]interface{}{ "one": "1", "two": 2, "three": float32(3.4), "four": uint8(5), "five": false, })) /* (map[string]string) (len=5) { (string) (len=3) "two": (string) (len=1) "2", (string) (len=5) "three": (string) (len=3) "3.4", (string) (len=4) "four": (string) (len=1) "5", (string) (len=4) "five": (string) (len=5) "false", (string) (len=3) "one": (string) (len=1) "1" } */ ``` ## ToStringMapStringSlice ```go= spew.Dump(cast.ToStringMapStringSlice(map[string][]interface{}{ "animal": {"dog", "cat"}, "bool": {true, false}, "card": {1, 2, 3, 4, 5}, })) /* (map[string][]string) (len=3) { (string) (len=4) "card": ([]string) (len=5 cap=8) { (string) (len=1) "1", (string) (len=1) "2", (string) (len=1) "3", (string) (len=1) "4", (string) (len=1) "5" }, (string) (len=6) "animal": ([]string) (len=2 cap=2) { (string) (len=3) "dog", (string) (len=3) "cat" }, (string) (len=4) "bool": ([]string) (len=2 cap=2) { (string) (len=4) "true", (string) (len=5) "false" } } */ ``` ![](https://i.imgur.com/GHBs2Ds.png) ![](https://i.imgur.com/CLFBMOh.png) {%hackmd theme-dark %}