to

package module
v0.0.0-...-d60d31e Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2020 License: MIT Imports: 5 Imported by: 77

README

to

The to package provides quick-and-dirty conversions between built-in Go data types. When any conversion is 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 github.com/xiam/to

Usage

Import the package

import "github.com/xiam/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(),to.Int() 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 receives interface{} as first argument and reflect.Kind as second:

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

Date formats and durations are matched against common patterns and converted:

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

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

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

Benchmarks

go test -bench=.

goos: linux
goarch: amd64
pkg: github.com/xiam/to
BenchmarkFmtIntToString-4         	11385524	       105 ns/op
BenchmarkFmtFloatToString-4       	 2266578	       531 ns/op
BenchmarkStrconvIntToString-4     	202011031	         5.88 ns/op
BenchmarkStrconvFloatToString-4   	 2515765	       474 ns/op
BenchmarkIntToString-4            	16711124	        77.0 ns/op
BenchmarkFloatToString-4          	 5648437	       214 ns/op
BenchmarkIntToBytes-4             	19158598	        70.9 ns/op
BenchmarkBoolToString-4           	167781417	         7.11 ns/op
BenchmarkFloatToBytes-4           	 6133180	       196 ns/op
BenchmarkIntToBool-4              	11871574	       106 ns/op
BenchmarkStringToTime-4           	  211500	      4997 ns/op
BenchmarkConvert-4                	13155015	        92.9 ns/op
PASS
ok  	github.com/xiam/to	17.887s

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-today José Nieto, https://xiam.dev

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 provides quick-and-dirty conversions between built-in Go data types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(value interface{}) bool

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

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

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

func Int

func Int(val interface{}) int

Int tries to convert the argument into an golang int. Returns int(0) if any error occurs.

func Int64

func Int64(val interface{}) int64

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

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

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

func Uint64

func Uint64(val interface{}) uint64

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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