to

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2016 License: MIT Imports: 5 Imported by: 5

README

gosexy/to

Convenient functions for converting values between common Go datatypes. For Go 1.1+.

This package ignores errors and allows quick-and-dirty conversions between Go datatypes. When any conversion seems unreasonable a zero value is used as fallback.

If you're not working with human provided data, fuzzy input or if you'd rather not ignore any error in your program, you should better use the standard Go packages for conversion, such as strconv, fmt or even standard conversion they may be better suited for the task.

Build Status

Installation

go get -u menteslibres.net/gosexy/to

Usage

Import the package

import "menteslibres.net/gosexy/to"

Use the available to functions to convert a float64 into a string:

// "1.23"
s := to.String(1.23)

Or a bool into string:

// "true"
s := to.String(true)

What about the other way around? string to float64 and string to bool.

// 1.23
f := to.Float64("1.23")

// true
b := to.Bool("true")

Note that this package only provides to.Uint64(), to.Int64() and to.Float64() but no to.Uint8(), to.Uint() or to.Float32() functions, if you'd like to produce a float32 instead of a float64 you'd first use to.Float64() and then cast the output using float32().

f32 := float32(to.Float64("12.34"))

There is another important function, to.Convert() that accepts any value (interface{}) as first argument and also a reflect.Kind, as second, that defines the data type the first argument will be converted to, this is also the only function that returns an error value.

val, err := to.Convert("12345", reflect.Int64)

Date formats and durations are also handled, you can use many fuzzy date formats and they would be converted into time.Time values.

timeVal = to.Time("2012-03-24")
timeVal = to.Time("Mar 24, 2012")

durationVal := to.Duration("12s37ms")

Now, an important question: how fast is this library compared to standard methods, like the fmt or strconv packages?

It is, of course, a little slower that strconv methods but it is faster than fmt, so it provides an acceptable speed for most projects. You can test it by yourself:

$ go test -test.bench=.
PASS
BenchmarkFmtIntToString           5000000               547 ns/op
BenchmarkFmtFloatToString         2000000               914 ns/op
BenchmarkStrconvIntToString      10000000               142 ns/op
BenchmarkStrconvFloatToString     1000000              1155 ns/op
BenchmarkIntToString             10000000               325 ns/op
BenchmarkFloatToString            2000000               873 ns/op
BenchmarkIntToBytes              10000000               198 ns/op
BenchmarkBoolToString            50000000                48.0 ns/op
BenchmarkFloatToBytes             2000000               773 ns/op
BenchmarkIntToBool                5000000               403 ns/op
BenchmarkStringToTime             1000000              1063 ns/op
BenchmarkConvert                 10000000               199 ns/op
ok      menteslibres.net/gosexy/to      27.670s

See the docs for a full reference of all the available to methods.

License

This is Open Source released under the terms of the MIT License:

Copyright (c) 2013-2014 José Carlos Nieto, https://menteslibres.net/xiam

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package to is a helper package for converting between datatypes.

If a certain value can not be directly converted to another, the zero value of the destination type is returned instead.

Index

Constants

View Source
const (
	// KindTime is reserved for Time kind.
	KindTime reflect.Kind = iota + 1000000000
	// KindDuration is reserved for Duration kind.
	KindDuration
)

Variables

This section is empty.

Functions

func AsBool

func AsBool(v interface{}) bool

AsBool converts v to bool or returns false.

func AsDuration

func AsDuration(v interface{}) time.Duration

AsDuration converts v to Duration or returns Duration(0).

func AsFloat

func AsFloat(v interface{}) float64

AsFloat converts v to float64 or returns float64(0)

func AsInt

func AsInt(v interface{}) int

AsInt converts v to int or returns 0.

func AsInt64

func AsInt64(v interface{}) int64

AsInt64 converts v to int64 or returns 0.

func AsMap

func AsMap(v interface{}) map[string]interface{}

AsMap converts v to map[string]interface{} or returns val.

func AsSlice

func AsSlice(v interface{}, val []interface{}) []interface{}

AsSlice converts v to []interface{} or returns val.

func AsString

func AsString(v interface{}) string

AsString converts v to string or returns ""

func AsTime

func AsTime(v interface{}) time.Time

AsTime converts v to Time or returns Time{}

func Bool

func Bool(value interface{}) (bool, error)

Bool tries to convert the argument into a bool. Returns false if any error occurs.

func Bytes

func Bytes(val interface{}) []byte

Bytes tries to convert the argument into a []byte array. Returns []byte{} if any error occurs.

func Convert

func Convert(value interface{}, t reflect.Kind) (interface{}, error)

Convert tries to convert the argument into a reflect.Kind element.

func Duration

func Duration(val interface{}) (time.Duration, error)

Duration tries to convert the argument into a time.Duration value. Returns time.Duration(0) if any error occurs.

func Float64

func Float64(val interface{}) (float64, error)

Float64 tries to convert the argument into a float64. Returns float64(0.0) if any error occurs.

func Int64

func Int64(val interface{}) (int64, error)

Int64 tries to convert the argument into an int64. Returns int64(0) if any error occurs.

func Map

func Map(val interface{}) (map[string]interface{}, error)

Map ...

func MustBool

func MustBool(v interface{}) bool

MustBool converts v to bool or panics.

func MustDuration

func MustDuration(v interface{}) time.Duration

MustDuration converts v to Duration or panics.

func MustFloat

func MustFloat(v interface{}) float64

MustFloat converts v to float64 or panics.

func MustInt

func MustInt(v interface{}) int

MustInt convert v to int64 or panics.

func MustInt64

func MustInt64(v interface{}) int64

MustInt64 convert v to int64 or panics.

func MustMap

func MustMap(v interface{}, val map[string]interface{}) map[string]interface{}

MustMap converts v to map[string]interface{} or returns val.

func MustSlice

func MustSlice(v interface{}, val []interface{}) []interface{}

MustSlice converts v to []interface{} or returns val.

func MustString

func MustString(v interface{}) string

MustString converts v to iterface{}

func MustTime

func MustTime(v interface{}) time.Time

MustTime converts v to Time or panics.

func OrBool

func OrBool(v interface{}, val bool) bool

OrBool converts v to bool or returns val.

func OrDuration

func OrDuration(v interface{}, val time.Duration) time.Duration

OrDuration converts v to Duration or returns val.

func OrFloat

func OrFloat(v interface{}, val float64) float64

OrFloat converts v to float64 or returns val.

func OrInt

func OrInt(v interface{}, val int) int

OrInt converts v to int64 or returns val.

func OrInt64

func OrInt64(v interface{}, val int64) int64

OrInt64 converts v to int64 or returns val.

func OrMap

func OrMap(v interface{}, val map[string]interface{}) map[string]interface{}

OrMap converts v to map[string]interface{} or returns val.

func OrSlice

func OrSlice(v interface{}, val []interface{}) []interface{}

OrSlice converts v to []interface{} or returns val.

func OrString

func OrString(v interface{}) string

OrString converts v to string or returns ""

func OrTime

func OrTime(v interface{}, val time.Time) time.Time

OrTime converts v to Time or returns Time{}

func Slice

func Slice(val interface{}) ([]interface{}, error)

Slice ...

func String

func String(val interface{}) string

String tries to convert the argument into a string. Returns "" if any error occurs.

func Time

func Time(val interface{}) (time.Time, error)

Time converts a date string into a time.Time value, several date formats are tried.

func Uint64

func Uint64(val interface{}) (uint64, error)

Uint64 tries to convert the argument into an uint64. Returns uint64(0) if any error occurs.

func ZeroBool

func ZeroBool(v interface{}) bool

ZeroBool converts v to bool or returns false.

func ZeroDuration

func ZeroDuration(v interface{}) time.Duration

ZeroDuration converts v to Duration or returns Duration(0).

func ZeroFloat

func ZeroFloat(v interface{}) float64

ZeroFloat converts v to float64 or returns float64(0)

func ZeroInt

func ZeroInt(v interface{}) int

ZeroInt converts v to int64 or returns 0.

func ZeroInt64

func ZeroInt64(v interface{}) int64

ZeroInt64 converts v to int64 or returns 0.

func ZeroMap

func ZeroMap(v interface{}) map[string]interface{}

ZeroMap converts v to map[string]interface{} or returns val.

func ZeroSlice

func ZeroSlice(v interface{}, val []interface{}) []interface{}

ZeroSlice converts v to []interface{} or returns val.

func ZeroString

func ZeroString(v interface{}) string

ZeroString converts v to string or returns ""

func ZeroTime

func ZeroTime(v interface{}) time.Time

ZeroTime converts v to Time or returns Time{}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL