Skip to content

A helper package that converts basic data types in golang

License

Notifications You must be signed in to change notification settings

mdaliyan/govert

Repository files navigation

govert

example workflow Coverage Status Go Report Card godoc Go Walker License

govert is a no-dependancy packagheprovides 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.

About

A helper package that converts basic data types in golang

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages