Documentation
¶
Overview ¶
Package conv 提供了基础的类型转换功能。会尽最大可能地 将当前的值转换成指定类型的值。
conv.MustInt("123", 0) // 返回 123 的数值
conv.MustString(123, "")// 返回字符串 123
conv.Int("123", 0) // 返回 123 数值和 nil 的 error 接口
v := 5
conv.Value("3", reflect.ValueOf(v)) // 将 3 转换成数值,并写入 v 中.
Index ¶
- func Bool(val interface{}) (bool, error)
- func Bytes(val interface{}) ([]byte, error)
- func Float32(val interface{}) (float32, error)
- func Float64(val interface{}) (float64, error)
- func Int(val interface{}) (int, error)
- func Int8(val interface{}) (int8, error)
- func Int32(val interface{}) (int32, error)
- func Int64(val interface{}) (int64, error)
- func Map2Obj(src interface{}, dest interface{}, conv FieldConvert) error
- func MustBool(val interface{}, def bool) bool
- func MustBytes(val interface{}, def []byte) []byte
- func MustFloat32(val interface{}, def float32) float32
- func MustFloat64(val interface{}, def float64) float64
- func MustInt(val interface{}, def int) int
- func MustInt8(val interface{}, def int8) int8
- func MustInt32(val interface{}, def int32) int32
- func MustInt64(val interface{}, def int64) int64
- func MustSlice(val interface{}, def []interface{}) []interface{}
- func MustString(val interface{}, def string) string
- func MustUint(val interface{}, def uint) uint
- func MustUint8(val interface{}, def uint8) uint8
- func MustUint32(val interface{}, def uint32) uint32
- func MustUint64(val interface{}, def uint64) uint64
- func Obj2Map(obj interface{}, conv FieldConvert) (map[string]interface{}, error)
- func Slice(val interface{}) ([]interface{}, error)
- func String(val interface{}) (string, error)
- func Uint(val interface{}) (uint, error)
- func Uint8(val interface{}) (uint8, error)
- func Uint32(val interface{}) (uint32, error)
- func Uint64(val interface{}) (uint64, error)
- func Value(source interface{}, target reflect.Value) error
- type FieldConvert
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bool ¶
Bool 将 val 转换成 bool 类型或是在无法转换的情况下返回 error。 以下值被可以被正确转换:
123(true), 0(false),"-123"(true), "on"(true), "off"(false), "true"(true), "false"(false)
func Map2Obj ¶
func Map2Obj(src interface{}, dest interface{}, conv FieldConvert) error
Map2Obj 将 map 中的数据转换成一个结构中的数据。
func MustFloat32 ¶
MustFloat32 将 val 转换成 float32 类型或是在无法转换的情况下返回 def 参数。
func MustFloat64 ¶
MustFloat64 将 val 转换成 float64 类型或是在无法转换的情况下返回 def 参数。
func MustSlice ¶
func MustSlice(val interface{}, def []interface{}) []interface{}
MustSlice 将 val 转换成 slice 类型或是在无法转换的情况下返回 def 参数。
func MustString ¶
MustString 将 val 转换成 string 类型或是在无法转换的情况下返回 def 参数。
func MustUint32 ¶
MustUint32 将 val 转换成 uint32 类型或是在无法转换的情况下返回 def 参数。
func MustUint64 ¶
MustUint64 将 val 转换成 uint64 类型或是在无法转换的情况下返回 def 参数。
func Obj2Map ¶
func Obj2Map(obj interface{}, conv FieldConvert) (map[string]interface{}, error)
Obj2Map 将 obj 转换成 map,只能转换可导出的数据。
func Slice ¶
func Slice(val interface{}) ([]interface{}, error)
Slice 将 val 转换成 slice 类型或是在无法转换的情况下返回 error。
[]int, []interface{} 以及数组都可以转换。 []byte("123") 返回 []interface{}{byte(49),byte(50),byte(51)} "123" 返回 []interface{}{rune(49),rune(50),rune(51)}
Types ¶
type FieldConvert ¶
FieldConvert 字段转换 用于 map 转换到一个对象实例或是从一个对象实例转换到 map