zstrconv

package
v0.0.0-alpha.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrTypeSupported = errors.New("zstrconv: unsupported type")
)

Functions

func FormatAny

func FormatAny(a any) string

FormatAny returns formatted string of any primitive data type. For given value v=a.(type), following format is applied.

  • nil : "<nil>"
  • string : v
  • []byte : string(v)
  • bool : strconv.FormatBool(v)
  • int,int8,int16,int32,int64 : strconv.FormatInt(int64(v), 10)
  • uint,uint8,uint16,uint32,uint64 : strconv.FormatUint(uint64(v), 10)
  • float32 : strconv.FormatFloat(float64(v), 'g', -1, 32)
  • float64 : strconv.FormatFloat(float64(v), 'g', -1, 64)
  • complex64 : strconv.FormatComplex(complex128(v), 'g', -1, 64)
  • complex128 : strconv.FormatComplex(complex128(v), 'g', -1, 128)
  • fmt.Stringer : v.String()
  • others : fmt.Sprint(v)

func ParseNum

func ParseNum[T Number](s string) (T, error)

ParseNum parses primitive numbers and return the parsed value. If some error had occurred while parsing s, it is returned with the zero value of T. ParseNum should not be used when performance is important.

Example
package main

import (
	"fmt"

	"github.com/aileron-projects/go/zerrors"
	"github.com/aileron-projects/go/zstrconv"
)

func main() {
	fmt.Printf("%T\n", zerrors.Must(zstrconv.ParseNum[int]("-123")))
	fmt.Printf("%T\n", zerrors.Must(zstrconv.ParseNum[int8]("-123")))
	fmt.Printf("%T\n", zerrors.Must(zstrconv.ParseNum[int16]("-123")))
	fmt.Printf("%T\n", zerrors.Must(zstrconv.ParseNum[int32]("-123")))
	fmt.Printf("%T\n", zerrors.Must(zstrconv.ParseNum[int64]("-123")))
	fmt.Printf("%T\n", zerrors.Must(zstrconv.ParseNum[uint]("123")))
	fmt.Printf("%T\n", zerrors.Must(zstrconv.ParseNum[uint8]("123")))
	fmt.Printf("%T\n", zerrors.Must(zstrconv.ParseNum[uint16]("123")))
	fmt.Printf("%T\n", zerrors.Must(zstrconv.ParseNum[uint32]("123")))
	fmt.Printf("%T\n", zerrors.Must(zstrconv.ParseNum[uint64]("123")))
	fmt.Printf("%T\n", zerrors.Must(zstrconv.ParseNum[uintptr]("123")))
	fmt.Printf("%T\n", zerrors.Must(zstrconv.ParseNum[float32]("1.23")))
	fmt.Printf("%T\n", zerrors.Must(zstrconv.ParseNum[float64]("1.23")))
}
Output:

int
int8
int16
int32
int64
uint
uint8
uint16
uint32
uint64
uintptr
float32
float64

Types

type Number

type Number interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64
}

Number is a constraint that permits any numeric type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL