Simple, safe conversion of any type, including indirect/custom types.
Inspired by cast
go get -u github.com/shockerli/cvt
Method
__E()
: expect handle error, while unable to convert
cvt.IntE("12") // 12, nil
cvt.Float64E("12.34") // 12.34, nil
cvt.StringE(12.34) // "12.34", nil
cvt.BoolE("false") // false, nil
dereferencing pointer and reach the base type
type Name string
var name Name = "jioby"
cvt.StringE(name) // jioby, nil
cvt.StringE(&name) // jioby, nil
Method
__()
: ignore error, while convert failed, will return the zero value of type
cvt.Int("12") // 12(success)
cvt.Int(struct{}{}) // 0(failed)
return the default value, while convert failed
cvt.Int(struct{}{}, 12) // 12
cvt.Float("hello", 12.34) // 12.34
1000+ unit test cases, for more examples, see
*_test.go
- Bool
- BoolE
- Int
- IntE
- Int8
- Int8E
- Int16
- Int16E
- Int32
- Int32E
- Int64
- Int64E
- Uint
- UintE
- Uint8
- Uint8E
- Uint16
- Uint16E
- Uint32
- Uint32E
- Uint64
- Uint64E
- String
- StringE
- Float32
- Float32E
- Float64
- Float64E
- Time
- TimeE
ColumnsE
: the values from a single column in the input array/slice/map of struct/map,[]interface{}
FieldE
: the field value from map/struct,interface{}
KeysE
: the keys of map,[]interface{}
Slice
SliceE
: convert an interface to a[]interface{}
typeSliceIntE
: convert an interface to a[]int
typeSliceInt64E
: convert an interface to a[]int64
typeSliceFloat64E
: convert an interface to a[]float64
typeSliceStringE
: convert an interface to a[]string
type
This project is under the terms of the MIT license.