Documentation ¶
Overview ¶
Example (Convert) ¶
package main import ( "fmt" "github.com/recallsong/go-utils/conv" ) func main() { var val interface{} val = 123 fmt.Println(conv.ToInt(val, -1)) fmt.Println(conv.ToInt64(val, -1)) fmt.Println(conv.ToUint(val, 0)) fmt.Println(conv.ToUint64(val, 0)) fmt.Println(conv.ToFloat32(val, -1)) fmt.Println(conv.ToFloat64(val, -1)) fmt.Println(conv.ToBool(val, false)) val = true fmt.Println(conv.ToBool(val, false)) fmt.Println(conv.String(val)) }
Output: 123 123 123 123 123 123 false true true
Example (FormatFloat) ¶
package main import ( "fmt" "github.com/recallsong/go-utils/conv" ) func main() { // conv.CleanFloat("") fmt.Println(conv.FormatFloat64(1.25678, 2)) fmt.Println(conv.FormatFloat64(1.02567, 2)) fmt.Println(conv.FormatFloat64(1.0000, 2)) }
Output: 1.26 1.03 1
Example (Parse) ¶
package main import ( "fmt" "github.com/recallsong/go-utils/conv" ) func main() { fmt.Println(conv.ParseInt("100", -1)) fmt.Println(conv.ParseInt("error", -1)) fmt.Println(conv.ParseInt8("108", -1)) fmt.Println(conv.ParseInt16("116", -1)) fmt.Println(conv.ParseInt32("133", -1)) fmt.Println(conv.ParseInt64("164", -1)) fmt.Println(conv.ParseUint("100", 0)) fmt.Println(conv.ParseUint8("108", 0)) fmt.Println(conv.ParseUint16("116", 0)) fmt.Println(conv.ParseUint32("132", 0)) fmt.Println(conv.ParseUint64("164", 0)) fmt.Println(conv.ParseFloat32("100.999", -1)) fmt.Println(conv.ParseFloat64("104.666", -1)) fmt.Println(conv.ParseBool("true", true)) fmt.Println(conv.ParseBool("1", true)) fmt.Println(conv.ParseBool("0", true)) fmt.Println(conv.ParseBool("False", true)) fmt.Println(conv.ParseBool("FALSE", true)) }
Output: 100 -1 108 116 133 164 100 108 116 132 164 100.999 104.666 true true false false false
Example (Slice) ¶
package main import ( "fmt" "github.com/recallsong/go-utils/conv" ) func main() { is := []int{1, 2, 3, 4, 5} var inters []interface{} = conv.Ints(is).ToInterfaces() fmt.Println(inters) fs := []float32{1, 2, 3, 4, 5} var ints []int = conv.Float32s(fs).ToInts() fmt.Println(ints) }
Output: [1 2 3 4 5] [1 2 3 4 5]
Index ¶
- func Bool(v interface{}) (bool, error)
- func CleanFloat(value string) string
- func Duration(v interface{}) (time.Duration, error)
- func Float32(v interface{}) (float32, error)
- func Float64(v interface{}) (float64, error)
- func FormatFloat32(value float32, precision int) string
- func FormatFloat64(value float64, precision int) string
- func Int(v interface{}) (int, error)
- func Int16(v interface{}) (int16, error)
- func Int32(v interface{}) (int32, error)
- func Int64(v interface{}) (int64, error)
- func Int8(v interface{}) (int8, error)
- func ParseBool(str string, defVal bool) bool
- func ParseFloat32(str string, defVal float32) float32
- func ParseFloat64(str string, defVal float64) float64
- func ParseInt(str string, defVal int) int
- func ParseInt16(str string, defVal int16) int16
- func ParseInt32(str string, defVal int32) int32
- func ParseInt64(str string, defVal int64) int64
- func ParseInt8(str string, defVal int8) int8
- func ParseUint(str string, defVal uint) uint
- func ParseUint16(str string, defVal uint16) uint16
- func ParseUint32(str string, defVal uint32) uint32
- func ParseUint64(str string, defVal uint64) uint64
- func ParseUint8(str string, defVal uint8) uint8
- func String(v interface{}) string
- func ToBool(v interface{}, defVal bool) bool
- func ToDuration(v interface{}, defVal time.Duration) time.Duration
- func ToFloat32(v interface{}, defVal float32) float32
- func ToFloat64(v interface{}, defVal float64) float64
- func ToInt(v interface{}, defVal int) int
- func ToInt16(v interface{}, defVal int16) int16
- func ToInt32(v interface{}, defVal int32) int32
- func ToInt64(v interface{}, defVal int64) int64
- func ToInt8(v interface{}, defVal int8) int8
- func ToString(v interface{}) string
- func ToUint(v interface{}, defVal uint) uint
- func ToUint16(v interface{}, defVal uint16) uint16
- func ToUint32(v interface{}, defVal uint32) uint32
- func ToUint64(v interface{}, defVal uint64) uint64
- func ToUint8(v interface{}, defVal uint8) uint8
- func Uint(v interface{}) (uint, error)
- func Uint16(v interface{}) (uint16, error)
- func Uint32(v interface{}) (uint32, error)
- func Uint64(v interface{}) (uint64, error)
- func Uint8(v interface{}) (uint8, error)
- type Bools
- type Complex128s
- type Complex64s
- type Float32s
- func (s Float32s) ToFloat64s() []float64
- func (s Float32s) ToInt16s() []int16
- func (s Float32s) ToInt32s() []int32
- func (s Float32s) ToInt64s() []int64
- func (s Float32s) ToInt8s() []int8
- func (s Float32s) ToInterfaces() []interface{}
- func (s Float32s) ToInts() []int
- func (s Float32s) ToUint16s() []uint16
- func (s Float32s) ToUint32s() []uint32
- func (s Float32s) ToUint64s() []uint64
- func (s Float32s) ToUint8s() []uint8
- func (s Float32s) ToUints() []uint
- type Float64s
- func (s Float64s) ToFloat32s() []float32
- func (s Float64s) ToInt16s() []int16
- func (s Float64s) ToInt32s() []int32
- func (s Float64s) ToInt64s() []int64
- func (s Float64s) ToInt8s() []int8
- func (s Float64s) ToInterfaces() []interface{}
- func (s Float64s) ToInts() []int
- func (s Float64s) ToUint16s() []uint16
- func (s Float64s) ToUint32s() []uint32
- func (s Float64s) ToUint64s() []uint64
- func (s Float64s) ToUint8s() []uint8
- func (s Float64s) ToUints() []uint
- type Int16s
- func (s Int16s) ToFloat32s() []float32
- func (s Int16s) ToFloat64s() []float64
- func (s Int16s) ToInt32s() []int32
- func (s Int16s) ToInt64s() []int64
- func (s Int16s) ToInt8s() []int8
- func (s Int16s) ToInterfaces() []interface{}
- func (s Int16s) ToInts() []int
- func (s Int16s) ToUint16s() []uint16
- func (s Int16s) ToUint32s() []uint32
- func (s Int16s) ToUint64s() []uint64
- func (s Int16s) ToUint8s() []uint8
- func (s Int16s) ToUints() []uint
- type Int32s
- func (s Int32s) ToFloat32s() []float32
- func (s Int32s) ToFloat64s() []float64
- func (s Int32s) ToInt16s() []int16
- func (s Int32s) ToInt64s() []int64
- func (s Int32s) ToInt8s() []int8
- func (s Int32s) ToInterfaces() []interface{}
- func (s Int32s) ToInts() []int
- func (s Int32s) ToUint16s() []uint16
- func (s Int32s) ToUint32s() []uint32
- func (s Int32s) ToUint64s() []uint64
- func (s Int32s) ToUint8s() []uint8
- func (s Int32s) ToUints() []uint
- type Int64s
- func (s Int64s) ToFloat32s() []float32
- func (s Int64s) ToFloat64s() []float64
- func (s Int64s) ToInt16s() []int16
- func (s Int64s) ToInt32s() []int32
- func (s Int64s) ToInt8s() []int8
- func (s Int64s) ToInterfaces() []interface{}
- func (s Int64s) ToInts() []int
- func (s Int64s) ToUint16s() []uint16
- func (s Int64s) ToUint32s() []uint32
- func (s Int64s) ToUint64s() []uint64
- func (s Int64s) ToUint8s() []uint8
- func (s Int64s) ToUints() []uint
- type Int8s
- func (s Int8s) ToFloat32s() []float32
- func (s Int8s) ToFloat64s() []float64
- func (s Int8s) ToInt16s() []int16
- func (s Int8s) ToInt32s() []int32
- func (s Int8s) ToInt64s() []int64
- func (s Int8s) ToInterfaces() []interface{}
- func (s Int8s) ToInts() []int
- func (s Int8s) ToUint16s() []uint16
- func (s Int8s) ToUint32s() []uint32
- func (s Int8s) ToUint64s() []uint64
- func (s Int8s) ToUint8s() []uint8
- func (s Int8s) ToUints() []uint
- type Interfaces
- func (s Interfaces) ToBools() []bool
- func (s Interfaces) ToComplex128s() []complex128
- func (s Interfaces) ToComplex64s() []complex64
- func (s Interfaces) ToFloat32s() []float32
- func (s Interfaces) ToFloat64s() []float64
- func (s Interfaces) ToInt16s() []int16
- func (s Interfaces) ToInt32s() []int32
- func (s Interfaces) ToInt64s() []int64
- func (s Interfaces) ToInt8s() []int8
- func (s Interfaces) ToInts() []int
- func (s Interfaces) ToStrings() []string
- func (s Interfaces) ToUint16s() []uint16
- func (s Interfaces) ToUint32s() []uint32
- func (s Interfaces) ToUint64s() []uint64
- func (s Interfaces) ToUint8s() []uint8
- func (s Interfaces) ToUints() []uint
- type Ints
- func (s Ints) ToFloat32s() []float32
- func (s Ints) ToFloat64s() []float64
- func (s Ints) ToInt16s() []int16
- func (s Ints) ToInt32s() []int32
- func (s Ints) ToInt64s() []int64
- func (s Ints) ToInt8s() []int8
- func (s Ints) ToInterfaces() []interface{}
- func (s Ints) ToUint16s() []uint16
- func (s Ints) ToUint32s() []uint32
- func (s Ints) ToUint64s() []uint64
- func (s Ints) ToUint8s() []uint8
- func (s Ints) ToUints() []uint
- type Strings
- type Uint16s
- func (s Uint16s) ToFloat32s() []float32
- func (s Uint16s) ToFloat64s() []float64
- func (s Uint16s) ToInt32s() []int32
- func (s Uint16s) ToInt64s() []int64
- func (s Uint16s) ToInt8s() []int8
- func (s Uint16s) ToInterfaces() []interface{}
- func (s Uint16s) ToInts() []int
- func (s Uint16s) ToUint32s() []uint32
- func (s Uint16s) ToUint64s() []uint64
- func (s Uint16s) ToUint8s() []uint8
- func (s Uint16s) ToUints() []uint
- type Uint32s
- func (s Uint32s) ToFloat32s() []float32
- func (s Uint32s) ToFloat64s() []float64
- func (s Uint32s) ToInt16s() []int16
- func (s Uint32s) ToInt32s() []int32
- func (s Uint32s) ToInt64s() []int64
- func (s Uint32s) ToInt8s() []int8
- func (s Uint32s) ToInterfaces() []interface{}
- func (s Uint32s) ToInts() []int
- func (s Uint32s) ToUint16s() []uint16
- func (s Uint32s) ToUint64s() []uint64
- func (s Uint32s) ToUint8s() []uint8
- func (s Uint32s) ToUints() []uint
- type Uint64s
- func (s Uint64s) ToFloat32s() []float32
- func (s Uint64s) ToFloat64s() []float64
- func (s Uint64s) ToInt16s() []int16
- func (s Uint64s) ToInt32s() []int32
- func (s Uint64s) ToInt64s() []int64
- func (s Uint64s) ToInt8s() []int8
- func (s Uint64s) ToInterfaces() []interface{}
- func (s Uint64s) ToInts() []int
- func (s Uint64s) ToUint16s() []uint16
- func (s Uint64s) ToUint32s() []uint32
- func (s Uint64s) ToUint64s() []uint64
- func (s Uint64s) ToUint8s() []uint8
- func (s Uint64s) ToUints() []uint
- type Uint8s
- func (s Uint8s) ToFloat32s() []float32
- func (s Uint8s) ToFloat64s() []float64
- func (s Uint8s) ToInt16s() []int16
- func (s Uint8s) ToInt32s() []int32
- func (s Uint8s) ToInt64s() []int64
- func (s Uint8s) ToInt8s() []int8
- func (s Uint8s) ToInterfaces() []interface{}
- func (s Uint8s) ToInts() []int
- func (s Uint8s) ToUint16s() []uint16
- func (s Uint8s) ToUint32s() []uint32
- func (s Uint8s) ToUint64s() []uint64
- func (s Uint8s) ToUints() []uint
- type Uints
- func (s Uints) ToFloat32s() []float32
- func (s Uints) ToFloat64s() []float64
- func (s Uints) ToInt16s() []int16
- func (s Uints) ToInt32s() []int32
- func (s Uints) ToInt64s() []int64
- func (s Uints) ToInt8s() []int8
- func (s Uints) ToInterfaces() []interface{}
- func (s Uints) ToInts() []int
- func (s Uints) ToUint16s() []uint16
- func (s Uints) ToUint32s() []uint32
- func (s Uints) ToUint64s() []uint64
- func (s Uints) ToUint8s() []uint8
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseFloat32 ¶
ParseFloat32 解析字符串为float32类型的值,失败则返回defVal
func ParseFloat64 ¶
ParseFloat64 解析字符串为float64类型的值,失败则返回defVal
func ParseInt16 ¶
ParseInt16 解析字符串为in16类型的值,失败则返回defVal
func ParseInt32 ¶
ParseInt32 解析字符串为in32类型的值,失败则返回defVal
func ParseInt64 ¶
ParseInt64 解析字符串为int64类型的值,失败则返回defVal
func ParseUint16 ¶
ParseUint16 解析字符串为uint16类型的值,失败则返回defVal
func ParseUint32 ¶
ParseUint32 解析字符串为uint32类型的值,失败则返回defVal
func ParseUint64 ¶
ParseUint64 解析字符串为uint64类型的值,失败则返回defVal
func ParseUint8 ¶
ParseUint8 解析字符串为uint8类型的值,失败则返回defVal
func ToDuration ¶
ToDuration 将interface{}转换为time.Duration,失败则返回defVal
Types ¶
type Bools ¶
type Bools []bool
Bools 具有类型转换能力的[]bool类型
func (Bools) ToInterfaces ¶
func (s Bools) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型
type Complex128s ¶
type Complex128s []complex128
Complex128s 具有类型转换能力的[]complex64类型
func (Complex128s) ToInterfaces ¶
func (s Complex128s) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型
type Complex64s ¶
type Complex64s []complex64
Complex64s 具有类型转换能力的[]complex64类型
func (Complex64s) ToInterfaces ¶
func (s Complex64s) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型
type Float32s ¶
type Float32s []float32
Float32s 具有类型转换能力的[]float32类型
func (Float32s) ToInterfaces ¶
func (s Float32s) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型
type Float64s ¶
type Float64s []float64
Float64s 具有类型转换能力的[]float64类型
func (Float64s) ToInterfaces ¶
func (s Float64s) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型
type Int16s ¶
type Int16s []int16
Int16s 具有类型转换能力的[]int16类型
func (Int16s) ToInterfaces ¶
func (s Int16s) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型
type Int32s ¶
type Int32s []int32
Int32s 具有类型转换能力的[]int32类型
func (Int32s) ToInterfaces ¶
func (s Int32s) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型
type Int64s ¶
type Int64s []int64
Int64s 具有类型转换能力的[]int64类型
func (Int64s) ToInterfaces ¶
func (s Int64s) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型
type Int8s ¶
type Int8s []int8
Int8s 具有类型转换能力的[]int8类型
func (Int8s) ToInterfaces ¶
func (s Int8s) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型
type Interfaces ¶
type Interfaces []interface{}
func (Interfaces) ToComplex128s ¶
func (s Interfaces) ToComplex128s() []complex128
ToComplex128s 转换为[]int类型
func (Interfaces) ToComplex64s ¶
func (s Interfaces) ToComplex64s() []complex64
ToComplex64s 转换为[]int类型
type Ints ¶
type Ints []int
Ints 具有类型转换能力的[]int类型
func (Ints) ToInterfaces ¶
func (s Ints) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型
type Strings ¶
type Strings []string
Strings 具有类型转换能力的[]string类型
func (Strings) ToInterfaces ¶
func (s Strings) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}
type Uint16s ¶
type Uint16s []uint16
Uint16s 具有类型转换能力的[]uint16类型
func (Uint16s) ToInterfaces ¶
func (s Uint16s) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型
type Uint32s ¶
type Uint32s []int32
Uint32s 具有类型转换能力的[]uint32类型
func (Uint32s) ToInterfaces ¶
func (s Uint32s) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型
type Uint64s ¶
type Uint64s []uint64
Uint64s 具有类型转换能力的[]uint64类型
func (Uint64s) ToInterfaces ¶
func (s Uint64s) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型
type Uint8s ¶
type Uint8s []uint8
Uint8s 具有类型转换能力的[]uint8类型
func (Uint8s) ToInterfaces ¶
func (s Uint8s) ToInterfaces() []interface{}
ToInterfaces 转换为[]interface{}类型