Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goconv

A lightweight Go library for effortless type conversion — no errors, just sensible defaults.

Description

goconv simplifies type conversions in Go by providing a set of intuitive functions that never return errors. Instead of dealing with error handling for every conversion, goconv returns sensible default values (like 0, empty string, or false) when conversions fail. This makes your code cleaner and more readable, especially when working with dynamic data sources like JSON, user input, or configuration files.

Key Features:

  • Zero error handling required
  • Sensible defaults for failed conversions
  • Support for all common Go types (int, uint, float, string, bool)
  • Safe negative-to-unsigned conversions (clamps to 0)
  • Consistent API across all conversion functions

Installation

go get github.com/dharper7051/goconv

Usage

package main

import (
    "fmt"
    "github.com/dharper7051/goconv"
)

func main() {
    // String conversions
    fmt.Println(goconv.String(42))           // "42"
    fmt.Println(goconv.String(3.14159))      // "3.141590" (6 decimal places)
    fmt.Println(goconv.String(true))         // "true"
    fmt.Println(goconv.String(nil))          // ""

    // Integer conversions
    fmt.Println(goconv.Int("123"))           // 123
    fmt.Println(goconv.Int("invalid"))       // 0
    fmt.Println(goconv.Int(3.14))            // 3
    fmt.Println(goconv.Int64(42))            // 42

    // Unsigned integer conversions (negatives clamp to 0)
    fmt.Println(goconv.Uint(-5))             // 0
    fmt.Println(goconv.Uint("100"))          // 100
    fmt.Println(goconv.Uint64("999"))        // 999

    // Float conversions
    fmt.Println(goconv.Float64("3.14"))      // 3.14
    fmt.Println(goconv.Float32(42))          // 42.0
    fmt.Println(goconv.Float64("invalid"))   // 0.0

    // Boolean conversions
    fmt.Println(goconv.Bool(1))              // true
    fmt.Println(goconv.Bool(0))              // false
    fmt.Println(goconv.Bool("true"))         // true
    fmt.Println(goconv.Bool("false"))        // false
    fmt.Println(goconv.Bool("invalid"))      // false
}

Available Functions

  • String(value any) string - Convert any value to string
  • Int(value any) int - Convert to int
  • Int32(value any) int32 - Convert to int32
  • Int64(value any) int64 - Convert to int64
  • Uint(value any) uint - Convert to uint (negatives become 0)
  • Uint32(value any) uint32 - Convert to uint32 (negatives become 0)
  • Uint64(value any) uint64 - Convert to uint64 (negatives become 0)
  • Float32(value any) float32 - Convert to float32
  • Float64(value any) float64 - Convert to float64
  • Bool(value any) bool - Convert to bool

Default Behavior

When conversions fail or receive invalid input, goconv returns sensible defaults:

  • Numeric types: 0
  • String: "" (empty string)
  • Bool: false

This eliminates the need for error checking while maintaining predictable behavior.

About

A lightweight Go library for effortless type conversion — no errors, just sensible defaults.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages