Converting String to []string, or string to float64 could be handy sometimes, when your project has inconsistent input.
UNPOPULAR OPINION: As Go is strictly typed language, I am not fan of casting and converting like this, but sometimes it can be very handy while handling incosistent data!
package main
import (
"fmt"
"github.com/dpanic/convert"
)
func main() {
res := convert.ToInt("123")
fmt.Printf("%+v %t\n", res, res)
}
Go Playground is here https://go.dev/play/p/Z5phbxlWGg0
- Convert basic types to any other basic type
- Same as above but with * pointer
- ToBoolP -> *bool
- ToIntP -> *int
- ToInt64P -> *int64
- ToFloat32P -> *float32
- ToFloat64P -> *float64
- ToStringP -> *string
- ToString -> string
- ToInt -> int
- ToInt64 -> int64
- ToFloat -> float64
- ToBool -> bool
- ToSliceOfString -> []string
- ToSliceOfFloat -> []float64
- ToMapOfStrings -> map[string]string
- ToMapOfInterfaces -> map[string]interface{}
- ToSliceOfMap -> []map[string]string
- ToSliceOfMapOfInterfaces -> []map[string]interface{}
- ToSliceOfBool -> []bool
- ToMapOfStruct[T] -> map[string]T