Skip to content

Simple helpers to convert anything to anything

License

Notifications You must be signed in to change notification settings

go-mods/convert

Repository files navigation

convert

GoDoc Go Report Card License

Convert is a library for Go that provides easy and safe casting and conversion between different types. It's designed to be flexible, efficient, and easy to use in your Go projects.

Features

  • Convert between primitive types (string, int, float, bool, etc.)
  • Convert to and from time.Time and time.Duration
  • Convert to and from slices and maps
  • Support for custom type conversions
  • Comprehensive error handling
  • Zero dependencies (except for testing)

Installation

go get github.com/go-mods/convert

Usage

import "github.com/go-mods/convert"

// String conversions
str := convert.ToString(42)
num, err := convert.ToIntE("42")

// Time conversions
duration := convert.ToDuration("1h30m")
timeValue := convert.ToTime("2023-01-01T12:00:00Z")

// Slice conversions
intSlice := convert.ToIntSlice([]interface{}{1, "2", 3.0})

// Map conversions
stringMap := convert.ToStringMapString(map[interface{}]interface{}{"key": "value"})

// Custom conversions
customConverter := func(value interface{}) string {
    if v, ok := value.(CustomType); ok {
        s := v.CustomToString()
        return &s
    }
    return nil
}
result := convert.ToString(myCustomValue, customConverter)

Documentation

For detailed documentation and examples, please refer to the GoDoc.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

Simple helpers to convert anything to anything

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages