A robust Go utility library for parsing and converting various data types from interface{} values with comprehensive fallback mechanisms and error handling.
- Type-safe parsing with context support
- Fallback values via
ParseXDefaultfunctions - Subtype handling for custom types derived from basic types
- Interface support for
fmt.Stringerimplementations - Comprehensive error handling with structured errors
- Zero dependencies for runtime (only
github.com/bborbe/errors)
go get github.com/bborbe/parseimport (
"context"
"github.com/bborbe/parse"
)
// Parse with error handling
value, err := parse.ParseString(context.Background(), 42)
if err != nil {
// Handle error
}
fmt.Println(value) // "42"
// Parse with default fallback
value := parse.ParseStringDefault(context.Background(), invalidValue, "default")
fmt.Println(value) // "default"// Parse int
num, err := parse.ParseInt(context.Background(), "123")
if err != nil {
// Handle error
}
fmt.Println(num) // 123
// Parse int64 with default
num64 := parse.ParseInt64Default(context.Background(), "invalid", 0)
fmt.Println(num64) // 0// Parse bool
flag, err := parse.ParseBool(context.Background(), "true")
if err != nil {
// Handle error
}
fmt.Println(flag) // true
// Parse with default
flag := parse.ParseBoolDefault(context.Background(), "invalid", false)
fmt.Println(flag) // false// Parse float64
f, err := parse.ParseFloat64(context.Background(), "3.14")
if err != nil {
// Handle error
}
fmt.Println(f) // 3.14// Parse string array
strs, err := parse.ParseStrings(context.Background(), []interface{}{"a", "b", "c"})
if err != nil {
// Handle error
}
fmt.Println(strs) // ["a", "b", "c"]
// Parse int array with default
ints := parse.ParseIntArrayDefault(context.Background(), "invalid", []int{1, 2, 3})
fmt.Println(ints) // [1, 2, 3]// Parse time with custom format
t, err := parse.ParseTime(context.Background(), "2023-12-25", "2006-01-02")
if err != nil {
// Handle error
}
fmt.Println(t) // 2023-12-25 00:00:00 +0000 UTC// Convert to ASCII (removes diacritics)
ascii, err := parse.ParseASCII(context.Background(), "café")
if err != nil {
// Handle error
}
fmt.Println(ascii) // "cafe"The library supports custom types derived from basic types:
type MyString string
value, err := parse.ParseString(context.Background(), MyString("hello"))
// Works seamlessly with custom typesParseString(ctx, value) (string, error)- Parse to stringParseInt(ctx, value) (int, error)- Parse to intParseInt64(ctx, value) (int64, error)- Parse to int64ParseBool(ctx, value) (bool, error)- Parse to boolParseFloat64(ctx, value) (float64, error)- Parse to float64ParseTime(ctx, value, format) (time.Time, error)- Parse to time.TimeParseASCII(ctx, value) (string, error)- Convert to ASCII
ParseStrings(ctx, value) ([]string, error)- Parse to string arrayParseIntArray(ctx, value) ([]int, error)- Parse to int arrayParseInt64Array(ctx, value) ([]int64, error)- Parse to int64 array
All parse functions have corresponding ParseXDefault variants that return a fallback value on error:
ParseStringDefault(ctx, value, defaultValue) stringParseIntDefault(ctx, value, defaultValue) intParseBoolDefault(ctx, value, defaultValue) bool- etc.
Full API documentation: pkg.go.dev/github.com/bborbe/parse