as

package module
v0.0.0-...-20ffc35 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2025 License: MIT Imports: 7 Imported by: 1

README

as

Convert any value to a known type.

query := r.URL.Query()

as.Int(query.Get("page"))
as.Float(query.Get("price"))
as.Bool(query.Get("in_stock"))

Installation

go get github.com/AnatoleLucet/as

Usage

package main

import (
    "github.com/AnatoleLucet/as"
)

func main() {
    as.String(123)             // "123"
    as.String(1.234)           // "1.234"
    as.String(true)            // "true"
    as.String([]byte("hello")) // "hello"
    as.String([]int{1,2,3})    // "[1 2 3]"

    as.Int("123")              // 123
    as.Int(1.234)              // 1
    as.Int(true)               // 1
    as.Int([]byte("123"))      // 123

    as.Bool("true")            // true
    as.Bool("yes")             // true
    as.Bool(1)                 // true
    as.Bool("false")           // false
    as.Bool("no")              // false
    as.Bool(0)                 // false

    as.Slice(as.Int, []string{"1", "2"}) // []int{1, 2}
    as.Map(as.Self, as.Int, map[string]string{"one": "1"}) // map[string]int{"one": 1}

    // you can also use reflection based converters, but note that these are a bit slower:
    as.T[int]("1")         // 1
    as.Type(reflect.TypeOf(int(0)), "1") // 1
    as.TypeSlice(reflect.TypeOf(int(0)), []string{"1", "2"}) // []int{1, 2}
    as.TypeMap(reflect.TypeOf(string("")), reflect.TypeOf(int(0)), map[string]string{"one": "1"}) // map[string]int{"one": 1}

    // and many more
}
as.Value()

as.Value() is a special generic type, purpose built for cases where you want to abstract an arbitrary value in a known type.

// With as.Value you can store multiple types in a single slice.
values := []as.V{
    as.Value("hello"),
    as.Value(123),
    as.Value(1.234),
}

str, _ := values[0].String()
num, _ := values[1].Int()
flt, _ := values[2].Float()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any[T any](v T) any

func Bool

func Bool[T any](v T) (bool, error)

func Float

func Float(v any) (float64, error)

func Float32

func Float32[T any](v T) (float32, error)

func Float64

func Float64[T any](v T) (float64, error)

func Int

func Int[T any](v T) (int, error)

func Int8

func Int8[T any](v T) (int8, error)

func Int16

func Int16[T any](v T) (int16, error)

func Int32

func Int32[T any](v T) (int32, error)

func Int64

func Int64[T any](v T) (int64, error)

func Map

func Map[OldK comparable, NewK comparable, OldV any, NewV any](
	keyConverter func(OldK) (NewK, error),
	valConverter func(OldV) (NewV, error),
	v map[OldK]OldV,
) (map[NewK]NewV, error)

func Rune

func Rune[T any](v T) (rune, error)

func Self

func Self[T any](v T) (T, error)

func Slice

func Slice[T, U any](converter func(v any) (U, error), v T) ([]U, error)

Slice attempts to convert the input to a slice of type T.

func String

func String[T any](v T) (string, error)

func T

func T[T any](v any) (T, error)

T converts the given value to the specified type T.

func Type

func Type[T any](target reflect.Type, v T) (any, error)

Type converts the given value to the specified reflect.Type.

func TypeMap

func TypeMap[T any](keyTarget, elemTarget reflect.Type, v T) (any, error)

TypeMap converts keys and values of a map to the specified reflect.Types.

func TypeSlice

func TypeSlice[T any](target reflect.Type, v T) (any, error)

TypeSlice converts elements of a slice to the specified reflect.Type.

func Uint

func Uint[T any](v T) (uint, error)

func Uint8

func Uint8[T any](v T) (uint8, error)

func Uint16

func Uint16[T any](v T) (uint16, error)

func Uint32

func Uint32[T any](v T) (uint32, error)

func Uint64

func Uint64[T any](v T) (uint64, error)

Types

type V

type V struct {
	// contains filtered or unexported fields
}

func Value

func Value[T any](v T) V

func (V) Bool

func (g V) Bool() (bool, error)

func (V) Float

func (g V) Float() (float64, error)

func (V) Float32

func (g V) Float32() (float32, error)

func (V) Float64

func (g V) Float64() (float64, error)

func (V) Int

func (g V) Int() (int, error)

func (V) Int8

func (g V) Int8() (int8, error)

func (V) Int16

func (g V) Int16() (int16, error)

func (V) Int32

func (g V) Int32() (int32, error)

func (V) Int64

func (g V) Int64() (int64, error)

func (V) Rune

func (g V) Rune() (rune, error)

func (V) String

func (g V) String() (string, error)

func (V) Uint

func (g V) Uint() (uint, error)

func (V) Uint8

func (g V) Uint8() (uint8, error)

func (V) Uint16

func (g V) Uint16() (uint16, error)

func (V) Uint32

func (g V) Uint32() (uint32, error)

func (V) Uint64

func (g V) Uint64() (uint64, error)

func (V) Value

func (g V) Value() any

Jump to

Keyboard shortcuts

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