Documentation ¶
Overview ¶
Package conv provides fast and intuitive conversions across Go types.
Example ¶
All conversion functions accept any type of value for conversion, if unable to find a reasonable conversion path they will return the target types zero value and an error.
package main import ( "fmt" conv "github.com/cstockton/go-conv" ) func main() { // The zero value and a non-nil error is returned on failure. fmt.Println(conv.Int("Foo")) // Conversions are allowed as long as the underlying type is convertable, for // example: type MyString string fmt.Println(conv.Int(MyString("42"))) // 42, nil // Pointers will be dereferenced when appropriate. str := "42" fmt.Println(conv.Int(&str)) // 42, nil // You may infer values from the base type of a pointer, giving you one // function signature for all conversions. This may be convenient when the // types are not known until runtime and reflection must be used. var val int err := conv.Infer(&val, `42`) fmt.Println(val, err) // 42, nil }
Output: 0 cannot convert "Foo" (type string) to int 42 <nil> 42 <nil> 42 <nil>
Example (Numerics) ¶
Numeric conversion from other numeric values of an identical type will be returned without modification. Numeric conversions deviate slightly from Go when dealing with under/over flow. When performing a conversion operation that would overflow, we instead assign the maximum value for the target type. Similarly, conversions that would underflow are assigned the minimun value for that type, meaning unsigned integers are given zero values instead of spilling into large positive integers.
package main import ( "fmt" conv "github.com/cstockton/go-conv" ) func main() { // For more natural Float -> Integer when the underlying value is a string. // Conversion functions will always try to parse the value as the target type // first. If parsing fails float parsing with truncation will be attempted. fmt.Println(conv.Int("-123.456")) // -123 // This does not apply for unsigned integers if the value is negative. Instead // performing a more intuitive (to the human) truncation to zero. fmt.Println(conv.Uint("-123.456")) // 0 }
Output: -123 <nil> 0 <nil>
Example (Panics) ¶
In short, panics should not occur within this library under any circumstance. This obviously excludes any oddities that may surface when the runtime is not in a healthy state, i.e. uderlying system instability, memory exhaustion. If you are able to create a reproducible panic please file a bug report.
package main import ( "fmt" conv "github.com/cstockton/go-conv" ) func main() { // The zero value for the target type is always returned. fmt.Println(conv.Bool(nil)) fmt.Println(conv.Bool([][]int{})) fmt.Println(conv.Bool((chan string)(nil))) fmt.Println(conv.Bool((*interface{})(nil))) fmt.Println(conv.Bool((*interface{})(nil))) fmt.Println(conv.Bool((**interface{})(nil))) }
Output: false cannot convert <nil> (type <nil>) to bool false <nil> false <nil> false cannot convert (*interface {})(nil) (type *interface {}) to bool false cannot convert (*interface {})(nil) (type *interface {}) to bool false cannot convert (**interface {})(nil) (type **interface {}) to bool
Index ¶
- func Bool(from interface{}) (bool, error)
- func Duration(from interface{}) (time.Duration, error)
- func Float32(from interface{}) (float32, error)
- func Float64(from interface{}) (float64, error)
- func Infer(into, from interface{}) error
- func Int(from interface{}) (int, error)
- func Int16(from interface{}) (int16, error)
- func Int32(from interface{}) (int32, error)
- func Int64(from interface{}) (int64, error)
- func Int8(from interface{}) (int8, error)
- func String(from interface{}) (string, error)
- func Time(from interface{}) (time.Time, error)
- func Uint(from interface{}) (uint, error)
- func Uint16(from interface{}) (uint16, error)
- func Uint32(from interface{}) (uint32, error)
- func Uint64(from interface{}) (uint64, error)
- func Uint8(from interface{}) (uint8, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bool ¶
Bool will convert the given value to a bool, returns the default value of false if a conversion can not be made.
Example ¶
Bool conversion supports all the paths provided by the standard libraries strconv.ParseBool when converting from a string, all other conversions are simply true when not the types zero value. As a special case zero length map and slice types are also false, even if initialized.
package main import ( "fmt" "time" conv "github.com/cstockton/go-conv" ) func main() { // Bool conversion from other bool values will be returned without // modification. fmt.Println(conv.Bool(true)) fmt.Println(conv.Bool(false)) // Bool conversion from strings consider the following values true: // "t", "T", "true", "True", "TRUE", // "y", "Y", "yes", "Yes", "YES", "1" // // It considers the following values false: // "f", "F", "false", "False", "FALSE", // "n", "N", "no", "No", "NO", "0" fmt.Println(conv.Bool("T")) fmt.Println(conv.Bool("False")) // Bool conversion from other supported types will return true unless it is // the zero value for the given type. fmt.Println(conv.Bool(int64(123))) fmt.Println(conv.Bool(int64(0))) fmt.Println(conv.Bool(time.Duration(123))) fmt.Println(conv.Bool(time.Duration(0))) fmt.Println(conv.Bool(time.Now())) fmt.Println(conv.Bool(time.Time{})) // All other types will return false. fmt.Println(conv.Bool(struct{ string }{""})) }
Output: true <nil> false <nil> true <nil> false <nil> true <nil> false <nil> true <nil> false <nil> true <nil> false <nil> false cannot convert struct { string }{string:""} (type struct { string }) to bool
func Duration ¶
Duration will convert the given value to a time.Duration, returns the default value of 0ns if a conversion can not be made.
Example ¶
Duration conversion supports all the paths provided by the standard libraries time.ParseDuration when converting from strings, with a couple enhancements outlined below.
package main import ( "fmt" "time" conv "github.com/cstockton/go-conv" ) func main() { // Duration conversion from strings will first attempt to parse as a Go // duration value using ParseDuration, then fall back to numeric conventions. fmt.Println(conv.Duration("1h1m100ms")) // 1h1m0.1s fmt.Println(conv.Duration("3660100000000")) // 1h1m0.1s // Numeric conversions directly convert to time.Duration nanoseconds. fmt.Println(conv.Duration(3660100000000)) // 1h1m0.1s // Floats deviate from the numeric conversion rules, instead // separating the integer and fractional portions into seconds. fmt.Println(conv.Duration("3660.10")) // 1h1m0.1s fmt.Println(conv.Duration(float64(3660.10))) // 1h1m0.1s // Complex numbers are Float conversions using the real number. fmt.Println(conv.Duration(complex(3660.10, 0))) // 1h1m0.1s // Duration conversion from time.Duration and any numerical type will be // converted using a standard Go conversion. This includes strings fmt.Println(conv.Duration(time.Nanosecond)) // 1s fmt.Println(conv.Duration(byte(1))) // 1ns }
Output: 1h1m0.1s <nil> 1h1m0.1s <nil> 1h1m0.1s <nil> 1h1m0.1s <nil> 1h1m0.1s <nil> 1h1m0.1s <nil> 1ns <nil> 1ns <nil>
func Float32 ¶
Float32 will convert the given value to a float32, returns the default value of 0.0 if a conversion can not be made.
func Float64 ¶
Float64 will convert the given value to a float64, returns the default value of 0.0 if a conversion can not be made.
Example ¶
Float64 conversion from other float values of an identical type will be returned without modification. Float64 from other types follow the general numeric rules.
package main import ( "fmt" conv "github.com/cstockton/go-conv" ) func main() { fmt.Println(conv.Float64(float64(123.456))) // 123.456 fmt.Println(conv.Float64("-123.456")) // -123.456 fmt.Println(conv.Float64("1.7976931348623157e+308")) }
Output: 123.456 <nil> -123.456 <nil> 1.7976931348623157e+308 <nil>
func Infer ¶
func Infer(into, from interface{}) error
Infer will perform conversion by inferring the conversion operation from the base type of a pointer to a supported T.
Example:
var into int64 err := conv.Infer(&into, `12`) // into -> 12
See examples for more usages.
Example ¶
Infer will perform conversion by inferring the conversion operation from a pointer to a supported T of the `into` param. Since the value is assigned directly only a error value is returned, meaning no type assertions needed.
package main import ( "fmt" conv "github.com/cstockton/go-conv" ) func main() { // Infer requires a pointer to all types. var into int if err := conv.Infer(into, `42`); err != nil { fmt.Println(err) } if err := conv.Infer(&into, `42`); err == nil { fmt.Println(into) } // Same as above but using new() truth := new(bool) if err := conv.Infer(truth, `TRUE`); err != nil { fmt.Println("Failed!") } }
Output: cannot infer conversion for unchangeable 0 (type int) 42
func Int ¶
Int will convert the given value to a int, returns the default value of 0 if a conversion can not be made.
Example ¶
Int conversions follow the the general numeric rules.
package main import ( "fmt" "math" conv "github.com/cstockton/go-conv" ) func main() { fmt.Println(conv.Uint("123.456")) // 123 fmt.Println(conv.Uint("-123.456")) // 0 fmt.Println(conv.Uint8(uint64(math.MaxUint64))) // 255 }
Output: 123 <nil> 0 <nil> 255 <nil>
func Int16 ¶
Int16 will convert the given value to a int16, returns the default value of 0 if a conversion can not be made.
func Int32 ¶
Int32 will convert the given value to a int32, returns the default value of 0 if a conversion can not be made.
func Int64 ¶
Int64 will convert the given value to a int64, returns the default value of 0 if a conversion can not be made.
func Int8 ¶
Int8 will convert the given value to a int8, returns the default value of 0 if a conversion can not be made.
func String ¶
String will convert the given value to a string, returns the default value of "" if a conversion can not be made.
Example ¶
String conversion from any values outside the cases below will simply be the result of calling fmt.Sprintf("%v", value), meaning it can not fail. An error is still provided and you should check it to be future proof.
package main import ( "fmt" conv "github.com/cstockton/go-conv" ) func main() { // String conversion from other string values will be returned without // modification. fmt.Println(conv.String("Foo")) // As a special case []byte will also be returned after a Go string conversion // is applied. fmt.Println(conv.String([]byte("Foo"))) // String conversion from types that do not have a valid conversion path will // still have sane string conversion for troubleshooting. fmt.Println(conv.String(struct{ msg string }{"Foo"})) }
Output: Foo <nil> Foo <nil> {Foo} <nil>
func Time ¶
Time will convert the given value to a time.Time, returns the empty struct time.Time{} if a conversion can not be made.
Example ¶
Time conversion from other time values will be returned without modification.
package main import ( "fmt" "time" conv "github.com/cstockton/go-conv" ) func main() { // Time conversion from other time.Time values will be returned without // modification. fmt.Println(`Times:`) fmt.Println(conv.Time(time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC))) // Time conversion from strings will be passed through time.Parse using a // variety of formats. Strings that could not be parsed along with all other // values will return an empty time.Time{} struct. fmt.Println(`Strings:`) formats := []string{ `Mon, 02 Jan 2006 15:04:05`, `Mon, 02 Jan 2006 15:04:05 UTC`, `Mon, 2 Jan 2006 15:04:05`, `Mon, 2 Jan 2006 15:04:05 UTC`, `02 Jan 2006 15:04 UTC`, `2 Jan 2006 15:04:05`, `2 Jan 2006 15:04:05 UTC`, } for _, format := range formats { t, err := conv.Time(format) if err != nil { fmt.Println(`Conversion error: `, err) } fmt.Printf("%v <-- (%v)\n", t, format) } // Time conversion from types that do not have a valid conversion path will // return the zero value and an error. fmt.Println(`Errors:`) fmt.Println(conv.Time(1)) // cannot convert 1 (type int) to time.Time fmt.Println(conv.Time(true)) // cannot convert true (type bool) to time.Time }
Output: Times: 2006-01-02 15:04:05 +0000 UTC <nil> Strings: 2006-01-02 15:04:05 +0000 UTC <-- (Mon, 02 Jan 2006 15:04:05) 2006-01-02 15:04:05 +0000 UTC <-- (Mon, 02 Jan 2006 15:04:05 UTC) 2006-01-02 15:04:05 +0000 UTC <-- (Mon, 2 Jan 2006 15:04:05) 2006-01-02 15:04:05 +0000 UTC <-- (Mon, 2 Jan 2006 15:04:05 UTC) 2006-01-02 15:04:00 +0000 UTC <-- (02 Jan 2006 15:04 UTC) 2006-01-02 15:04:05 +0000 UTC <-- (2 Jan 2006 15:04:05) 2006-01-02 15:04:05 +0000 UTC <-- (2 Jan 2006 15:04:05 UTC) Errors: 0001-01-01 00:00:00 +0000 UTC cannot convert 1 (type int) to time.Time 0001-01-01 00:00:00 +0000 UTC cannot convert true (type bool) to time.Time
func Uint ¶
Uint will convert the given value to a uint, returns the default value of 0 if a conversion can not be made.
Example ¶
Uint conversions follow the the general numeric rules.
package main import ( "fmt" "math" conv "github.com/cstockton/go-conv" ) func main() { fmt.Println(conv.Uint("123.456")) // 123 fmt.Println(conv.Uint("-123.456")) // 0 fmt.Println(conv.Uint8(uint64(math.MaxUint64))) // 255 }
Output: 123 <nil> 0 <nil> 255 <nil>
func Uint16 ¶
Uint16 will convert the given value to a uint16, returns the default value of 0 if a conversion can not be made.
func Uint32 ¶
Uint32 will convert the given value to a uint32, returns the default value of 0 if a conversion can not be made.
Types ¶
This section is empty.
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
convert
Package convert contains common conversion interfaces.
|
Package convert contains common conversion interfaces. |
refconv
Package refconv implements the Converter interface by using the standard libraries reflection package.
|
Package refconv implements the Converter interface by using the standard libraries reflection package. |