Git Product home page Git Product logo

govert's Introduction

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.

govert's People

Contributors

mdaliyan avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.