govert

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2018 License: MIT Imports: 5 Imported by: 1

README

govert

Build Status Go Report Card GoDoc Go Walker License

Package govert provides you some helpers to convert golang basic data types specially interfaces to any another basic type.

Install:

$ go get github.com/mdaliyan/govert

Usage:

import govert as following, then you have 2 ways to use it.

import to "github.com/mdaliyan/govert"
1. Converting inline
package main
    
import to "github.com/mdaliyan/govert"
import "fmt"
    
func main() {
    
    var i interface{} = 3783.2882332
    
    // interface to string
    aString := to.String(i, 3) // "3783.288"
    
    // float64 to string
    anotherString := to.String(3783.2882332, 3) // "3783.288"
    
    // bool to string
    aTrueString := to.String(true)   // "true"
    aFalseString := to.String(false) // "false"
    
    // bool to int8
    aTrueInt8 := to.Int8(true)   // 1
    aFalseInt8 := to.Int8(false) // 0
    
    // string to float64
    aFloat32 := to.Float32("3783.2882332") // 3783.2883
    
    // string to int8
    anInt8 := to.Int8("56") // 56
    
    fmt.Println(aString, anotherString, aTrueString, aFalseString, aTrueInt8, aFalseInt8, aFloat32, anInt8)
    // 3783.288 3783.288 true false 1 0 3783.2883 56
}
2. Converting by passing destination pointer
package main
    
import to "github.com/mdaliyan/govert"
import "fmt"
    
func main() {
    
    var err error
    
    // string to float64
    var aFloat float64
    err = to.This("3783.2882332", &aFloat) // 3783.2882332
    
    // string to int64
    var anInt64 int64
    err = to.This("3783.2882332", &anInt64) // 3783
    
    // float64 to int32
    var anInt32 int32
    err = to.This(3783.2882332, &anInt32) // 3783
    
    // float64 to string
    var String1, String2, String3 string
    err = to.This(3783.2882332, &String1)    // "3783.2882", 4 percs by default
    err = to.This(3783.2882332, &String2, 3) // "3783.288"
    err = to.This(3783.2882332, &String3, 2) // "3783.29"
    
    fmt.Println(err, aFloat, anInt64, anInt32, String1, String2, String3)
    // <nil> 3783.2882332 3783 3783 3783.2882 3783.288 3783.29
}

Todo:

  • Support converting from complex type to other types. (At this point I'm thinking of just removing imaginary part.)
  • Adding benchmarks

Benchmarks:

govert uses reflection to detect data types so it may not be what you want if you have so many ops and you need code performance to be high. Otherwise it’s ok for simple use cases, build prototypes or learning purpose.

Converting numbers like int, in16, int32, in64, float32, float64, uint, uint, uint16, uint32, uint64 to each other with govert is totally non-optimal. Just convert them like this.

 var a float32 = 331.23
 var b int64 = int64(a)

If you have an interface and you have to check type before converting it, or you are trying to convert something to string or vice versa, using govert makes sense.

Converting govert regular way compare
From To ns/op B/op allocs/op ns/op B/op allocs/op times slower
int (interface) string 200 51 4 63.1 19 2 x3.2
int (interface) float64 119 24 3 22.0 8 1 x5.4
int string 216 64 5 59.6 19 2 x11.6
int float64 219 64 5 18.6 8 1 x12
string (interface) int 149 24 3 75 48 1 x2
string (interface) float64 150 24 3 45.3 8 1 x3.3
string int 185 40 4 71.5 48 1 x2.6
string float64 186 40 4 40.6 8 1 x4.5
* The above benchmark tests were ran using an Intel(R) Core(TM) i5-2520M CPU @ 2.50GHz

Contributing:

Feel free to send PR.

Documentation

Overview

Package govert provides you some helpers to convert basic data types specially interfaces to other basic types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(in interface{}) (out bool)

Bool converts basic types to bool.

// will return false if input interface is numeric and is equal to 0.
fmt.Println(govert.Bool(224.58719)) // true
fmt.Println(govert.Bool(0)) // false

// will return true if input interface is string and is equal to "true" or "1", compared case insensitive.
fmt.Println(govert.Bool("anything")) // false
fmt.Println(govert.Bool("false")) // false
fmt.Println(govert.Bool("True")) // true
fmt.Println(govert.Bool("true")) // true
fmt.Println(govert.Bool("1")) // true

func Complex128

func Complex128(in interface{}) (out complex128)

Complex128 converts basic types to complex128

func Complex64

func Complex64(in interface{}) (out complex64)

Complex64 converts basic types to complex64

func Float32

func Float32(in interface{}) (out float32)

Float32 converts basic types to float32

func Float64

func Float64(in interface{}) (out float64)

Float64 converts basic types to float64

func Int

func Int(in interface{}) (out int)

Int converts basic types to int

func Int16

func Int16(in interface{}) (out int16)

Int16 converts basic types to int16

func Int32

func Int32(in interface{}) (out int32)

Int32 converts basic types to int32

func Int64

func Int64(in interface{}) (out int64)

Int64 converts basic types to int64

func Int8

func Int8(in interface{}) (out int8)

Int8 converts basic types to in8

func String

func String(in interface{}, params ...interface{}) (out string)

String converts any type to string second parameter is used when you try to convert float to string as number of digits after decimal point. note that golang will round the result.

fmt.Println(govert.String(224.58719,2)) // 224.59
fmt.Println(govert.String(224.58719,3)) // 224.587

func This

func This(in, out interface{}, params ...interface{}) (err error)

This converts it's first parameter's value to type of it's second parameter and overwrites the second parameter with it

func Uint

func Uint(in interface{}) (out uint)

Uint converts basic types to uint

func Uint16

func Uint16(in interface{}) (out uint16)

Uint16 converts basic types to uint6

func Uint32

func Uint32(in interface{}) (out uint32)

Uint32 converts basic types to uint32

func Uint64

func Uint64(in interface{}) (out uint64)

Uint64 converts basic types to uint64

func Uint8

func Uint8(in interface{}) (out uint8)

Uint8 converts basic types to uint8

Types

This section is empty.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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