Documentation ¶
Overview ¶
Package as helps by converting various data types to various other types
Index ¶
- func Bool(valuea ...interface{}) bool
- func Bytes(valuea ...interface{}) []byte
- func DBType(str string) string
- func DBTypeMultiple(val []string) string
- func Duration(valuea ...interface{}) time.Duration
- func FixedLengthAfter(str string, spacer string, length int) string
- func FixedLengthBefore(str string, spacer string, length int) string
- func FixedLengthCenter(str string, spacer string, length int) string
- func Float(valuea ...interface{}) float64
- func FloatFromXString(valuea ...string) float64
- func Int(valuea ...interface{}) int64
- func String(valuea ...interface{}) string
- func Time(valuea ...interface{}) time.Time
- func Trimmed(valuea ...interface{}) string
- func Type(valuea ...interface{}) (string, error)
- func Uint(valuea ...interface{}) uint64
- type Dynamic
- type Int64
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bool ¶
func Bool(valuea ...interface{}) bool
Bool returns a boolean value. It mainly depends on the output of strconv.ParseBool, but also checks for integer values.
Example ¶
package main import ( "fmt" "simonwaldherr.de/go/golibs/as" ) func main() { values := []interface{}{"foobar", "true", 1, true} fmt.Println("as.Bool()") for _, v := range values { fmt.Printf("%v => %v\n", v, as.Bool(v)) } }
Output: as.Bool() foobar => false true => true 1 => true true => true
func Bytes ¶
func Bytes(valuea ...interface{}) []byte
Bytes returns a slice of bytes.
Example ¶
package main import ( "fmt" "simonwaldherr.de/go/golibs/as" ) func main() { values := []interface{}{"foobar", "true", 1, true} fmt.Println("as.Bytes()") for _, v := range values { fmt.Printf("%v => %v\n", v, as.Bytes(v)) } }
Output: as.Bytes() foobar => [102 111 111 98 97 114] true => [116 114 117 101] 1 => [49] true => [116 114 117 101]
func DBTypeMultiple ¶
DBTypeMultiple returns the lowest common denominator of a type for all inserted DBTypes
func Duration ¶
Duration converts input values to time.Duration. It mainly depends on time.ParseDuration.
func FixedLengthAfter ¶
FixedLengthAfter appends spacer chars after a string
func FixedLengthBefore ¶
FixedLengthBefore prepends spacer chars before a string
func FixedLengthCenter ¶
FixedLengthCenter adds spacer chars after and before a string
func Float ¶
func Float(valuea ...interface{}) float64
Float converts it's input to type float64. int, uint and float gets converted as expected, time is transformed to a float of the corresponding timestamp. strings and byte slices gets converted via strconv.ParseFloat.
func FloatFromXString ¶
FloatFromXString converts strings to float64. Most values can be converted to float via Float(), but floats as strings in e.g. german spelling should be converted with this function.
func Int ¶
func Int(valuea ...interface{}) int64
Int returns an int64 of the input value. Float values and float values in strings will be rounded via "round half towards positive infinity". strings get converted via strconv.ParseFloat.
func String ¶
func String(valuea ...interface{}) string
String converts input values to string. Time and Duration gets converted via standard functions. Most types gets "converted" via fmt.Sprintf.
Example ¶
package main import ( "fmt" "time" "simonwaldherr.de/go/golibs/as" ) func main() { values := []interface{}{[]byte("foobar"), 1, true, 5 * time.Second, 0xfefe} fmt.Println("as.String()") for _, v := range values { fmt.Printf("%v => %v\n", v, as.String(v)) } }
Output: as.String() [102 111 111 98 97 114] => foobar 1 => 1 true => true 5s => 5s 65278 => 65278
func Time ¶
Time converts inputs values to time.Time. Time formats in the variable timeformats can be used.
Example ¶
package main import ( "fmt" "simonwaldherr.de/go/golibs/as" ) func main() { values := []interface{}{"01.01.1970", "01.01.1999", "2000/10/30"} fmt.Println("as.Time()") for _, v := range values { fmt.Printf("%v => %v\n", v, as.Time(v)) } }
Output: as.Time() 01.01.1970 => 1970-01-01 00:00:00 +0000 UTC 01.01.1999 => 1999-01-01 00:00:00 +0000 UTC 2000/10/30 => 2000-10-30 00:00:00 +0000 UTC
func Trimmed ¶
func Trimmed(valuea ...interface{}) string
Trimmed takes the first given value, converts it to a string, trims the whitespace an returns it.
func Type ¶
Type returns a type (string) of a string.
Example ¶
package main import ( "fmt" "simonwaldherr.de/go/golibs/as" ) func main() { values := []string{ "false", "0", "3", "5€", "23$", "42¢", "1.337£", "020161586X", "13,37", "https://simonwaldherr.de/", "contact@simonwaldherr.de", "127.0.0.1", "rgb(255, 231, 200)", "#ffffff", "#03a", "06.06.1989", "2010/07/29", "2006-01-02", "¢«∑€®†Ω¨⁄ø𕱑æœ@∆ºª©ƒ∂‚å≤¥≈ç√∫~µ∞…–¡“≠¿'¬”fi", } fmt.Println("as.Type()") for _, v := range values { str, _ := as.Type(v) fmt.Printf("%v => %v\n", v, str) } }
Output: as.Type() false => bool 0 => bool 3 => int 5€ => price 23$ => price 42¢ => price 1.337£ => price 020161586X => isbn 13,37 => float https://simonwaldherr.de/ => url contact@simonwaldherr.de => email 127.0.0.1 => ipv4 rgb(255, 231, 200) => color #ffffff => color #03a => color 06.06.1989 => date 2010/07/29 => date 2006-01-02 => date ¢«∑€®†Ω¨⁄ø𕱑æœ@∆ºª©ƒ∂‚å≤¥≈ç√∫~µ∞…–¡“≠¿'¬”fi =>
Example (Json) ¶
package main import ( "encoding/json" "fmt" "simonwaldherr.de/go/golibs/as" ) type dynStruct struct { First as.Dynamic `json:"first"` Second as.Dynamic `json:"second"` Third as.Dynamic `json:"third"` } func main() { jsonStr := ` { "first": "2019-01-04 00:00:00", "second": "foobar@example.tld", "third": "15€" }` x := dynStruct{} if err := json.Unmarshal([]byte(jsonStr), &x); err != nil { fmt.Println(err) } fmt.Printf("%v: %v\n", x.First.Type, x.First.Data) fmt.Printf("%v: %v\n", x.Second.Type, x.Second.Data) fmt.Printf("%v: %v\n", x.Third.Type, x.Third.Data) }
Output: date: 2019-01-04 00:00:00 +0000 UTC email: foobar@example.tld price: 15€
Types ¶
type Dynamic ¶ added in v0.10.2
type Dynamic struct { Type string Data interface{} }
func (*Dynamic) UnmarshalJSON ¶ added in v0.10.2
UnmarshalJSON implements json.Unmarshaler inferface for the Dynamic Type.
type Int64 ¶ added in v0.10.2
type Int64 int64
func (Int64) MarshalJSON ¶ added in v0.10.2
MarshalJSON implements json.Marshaler interface for the Int64 Type.
func (*Int64) UnmarshalJSON ¶ added in v0.10.2
UnmarshalJSON implements json.Unmarshaler inferface for the Int64 Type.