Documentation
¶
Overview ¶
Package cast provides generic type conversion for Go 1.21+.
The two public entry points are To (ignores errors) and ToE (returns errors). Both accept an optional variadic Op list that controls conversion behavior; see Flag for available options.
Supported target types are described by the Types constraint: all basic scalar types, slices of scalars, channels of scalars/slices, maps, and Func wrappers for each of those groups.
Index ¶
Examples ¶
- To (BigFloat)
- To (BigInt)
- To (BigIntHex)
- To (Bool)
- To (Chan)
- To (ChanLength)
- To (ChanSlice)
- To (Complex128)
- To (Complex64)
- To (DecodeScalar)
- To (DecodeSlice)
- To (Duration)
- To (DurationFromInt)
- To (ErrorTarget)
- To (Float64)
- To (Func)
- To (FuncChan)
- To (Int)
- To (Map)
- To (MapFromJSONString)
- To (MapFromMap)
- To (MapFromPrivateStruct)
- To (MapFromSlice)
- To (MapFromStruct)
- To (MapToPrivateStruct)
- To (MapToStruct)
- To (MapToStructTags)
- To (NetIP)
- To (NetIPFromUint32)
- To (Regexp)
- To (Slice)
- To (SliceUniqueValues)
- To (String)
- To (StringFromBytes)
- To (StringJson)
- To (StringerTarget)
- To (Struct)
- To (StructNested)
- To (StructToMap)
- To (StructToMapNonStrict)
- To (StructToStruct)
- To (StructToStructJsonTags)
- To (StructToStructPrivate)
- To (Time)
- To (TimeFormat)
- To (TimeFromFloat)
- To (TimeFromInt)
- To (UintAbs)
- To (Url)
- ToE (BigFloat)
- ToE (BigInt)
- ToE (BoolErr)
- ToE (ChanErr)
- ToE (Complex128)
- ToE (DecodeErr)
- ToE (Duration)
- ToE (ErrorTarget)
- ToE (ErrorWithDefault)
- ToE (Float64)
- ToE (FuncErr)
- ToE (Int)
- ToE (MapDuplicateKeyError)
- ToE (MapErr)
- ToE (NetIP)
- ToE (Regexp)
- ToE (SliceErr)
- ToE (String)
- ToE (StringerTarget)
- ToE (StructStrict)
- ToE (Time)
- ToE (TimeFormat)
- ToE (UintErr)
- ToE (Url)
Constants ¶
const ( DEFAULT = internal.DEFAULT ABS = internal.ABS DECODE = internal.DECODE DUPLICATE_KEY_ERROR = internal.DUPLICATE_KEY_ERROR FORMAT = internal.FORMAT JSON = internal.JSON LENGTH = internal.LENGTH PRIVATE = internal.PRIVATE STRICT = internal.STRICT UNIQUE_VALUES = internal.UNIQUE_VALUES )
Available option flags. See the internal package for full documentation.
Variables ¶
var ( // ErrorUnableToCast is a sentinel error returned when a value cannot be // cast to the requested type. ErrorUnableToCast = internal.ErrorUnableToCast // ErrorSignedToUnsigned is returned when a negative value is cast to an // unsigned integer type and the ABS flag is not set. ErrorSignedToUnsigned = internal.ErrorSignedToUnsigned // ErrorInvalidOption is a format string (not an error value) used to build // error messages when an Op flag carries an unexpected value type. ErrorInvalidOption = internal.ErrorInvalidOption // ErrorStrErrorCastingFunc is a format string (not an error value) used // when an element cast fails inside a Func[T] closure generator. ErrorStrErrorCastingFunc = internal.ErrorStrErrorCastingFunc // ErrorStrUnableToCast is a format string (not an error value) used when // a value cannot be converted to the requested target type. ErrorStrUnableToCast = internal.ErrorStrUnableToCast // Error is a deprecated alias for [ErrorUnableToCast]. // // Deprecated: use [ErrorUnableToCast]. Error = ErrorUnableToCast )
Sentinel errors and reusable format strings used throughout the package. These are re-exports of the values defined in the internal package so that callers see them through the cast namespace while the conversion code lives behind the internal/ boundary.
Functions ¶
func To ¶
To casts the value v to the given type, ignoring any errors. See the ToE documentation for more information.
Example (BigFloat) ¶
f := cast.To[*big.Float]("3.14")
fmt.Printf("%v", f.Text('f', 2))
Output: 3.14
Example (BigInt) ¶
n := cast.To[*big.Int]("123456789012345678901234567890")
fmt.Printf("%v", n)
Output: 123456789012345678901234567890
Example (BigIntHex) ¶
// Base is auto-detected: 0x=hex, 0o=octal, 0b=binary, else decimal.
n := cast.To[*big.Int]("0xff")
fmt.Printf("%v", n)
Output: 255
Example (Bool) ¶
fmt.Println(cast.To[bool](1)) fmt.Println(cast.To[bool](0))
Output: true false
Example (Chan) ¶
ch := cast.To[chan int]("10")
v := <-ch
fmt.Printf("%v (%T)", v, v)
Output: 10 (int)
Example (ChanLength) ¶
ch := cast.To[chan int](10, cast.Op{cast.LENGTH, 5})
v := <-ch
fmt.Printf("%v (cap %d)", v, cap(ch))
Output: 10 (cap 5)
Example (ChanSlice) ¶
ch := cast.To[chan []int]([]string{"1", "2", "3"})
fmt.Printf("%v (%T)", <-ch, ch)
Output: [1 2 3] (chan []int)
Example (Complex128) ¶
v := cast.To[complex128](3.14)
fmt.Printf("%v", v)
Output: (3.14+0i)
Example (Complex64) ¶
v := cast.To[complex64](float32(1.5))
fmt.Printf("%v", v)
Output: (1.5+0i)
Example (DecodeScalar) ¶
// DECODE=json unwraps a JSON-encoded string before converting.
v := cast.To[int](`"42"`, cast.Op{cast.DECODE, "json"})
fmt.Printf("%v", v)
Output: 42
Example (DecodeSlice) ¶
// DECODE=json forces JSON decoding for a slice target.
v := cast.To[[]int](`[1,2,3]`, cast.Op{cast.DECODE, "json"})
fmt.Printf("%v", v)
Output: [1 2 3]
Example (Duration) ¶
d := cast.To[time.Duration]("1h30m")
fmt.Printf("%v", d)
Output: 1h30m0s
Example (DurationFromInt) ¶
// int64 input is nanoseconds.
d := cast.To[time.Duration](int64(5_000_000_000))
fmt.Printf("%v", d)
Output: 5s
Example (ErrorTarget) ¶
err := fmt.Errorf("something failed")
result := cast.To[error](err)
fmt.Printf("%v", result)
Output: something failed
Example (Float64) ¶
v := cast.To[float64]("1.234")
fmt.Printf("%#v (%T)", v, v)
Output: 1.234 (float64)
Example (Func) ¶
f := cast.To[cast.Func[int]]("10")
fmt.Printf("%v (%T)", f(), f())
Output: 10 (int)
Example (FuncChan) ¶
f := cast.To[cast.Func[chan int]](42)
fmt.Printf("%v", <-f())
Output: 42
Example (Int) ¶
v := cast.To[int]("1")
fmt.Printf("%#v (%T)", v, v)
Output: 1 (int)
Example (Map) ¶
m := cast.To[map[string]int](map[string]string{"a": "1", "b": "2"})
fmt.Printf("a=%v b=%v (%T)", m["a"], m["b"], m)
Output: a=1 b=2 (map[string]int)
Example (MapFromJSONString) ¶
m := cast.To[map[string]int](`{"a":1,"b":2}`)
fmt.Printf("a=%v b=%v", m["a"], m["b"])
Output: a=1 b=2
Example (MapFromMap) ¶
m := cast.To[map[string]int](map[string]string{"a": "1"})
fmt.Printf("%v (%T)", m["a"], m["a"])
Output: 1 (int)
Example (MapFromPrivateStruct) ¶
// PRIVATE includes unexported fields as map keys.
type Point struct{ x, y int }
m := cast.To[map[string]any](Point{x: 3, y: 4}, cast.Op{cast.PRIVATE, true})
fmt.Printf("x=%v y=%v", m["x"], m["y"])
Output: x=3 y=4
Example (MapFromSlice) ¶
m := cast.To[map[int]string]([]string{"a", "b", "c"})
fmt.Printf("%v (%T)", m[0], m[0])
Output: a (string)
Example (MapFromStruct) ¶
type Point struct{ X, Y int }
m := cast.To[map[string]any](Point{X: 3, Y: 4})
fmt.Printf("X=%v Y=%v", m["X"], m["Y"])
Output: X=3 Y=4
Example (MapToPrivateStruct) ¶
// PRIVATE allows map values to be written into unexported struct fields.
type Point struct{ x, y int }
p := cast.To[Point](map[string]any{"x": 3, "y": 4}, cast.Op{cast.PRIVATE, true})
fmt.Printf("x=%v y=%v (%T)", p.x, p.y, p)
Output: x=3 y=4 (cast_test.Point)
Example (MapToStruct) ¶
type MyStruct struct {
X int
Y int
A string
B string
}
p := cast.To[MyStruct](map[string]string{
"X": "3", "Y": "4", "A": "hello", "B": "world",
})
fmt.Printf("p=(%T), X=%v (%T), Y=%v (%T), A=%v (%T), B=%v (%T)",
p, p.X, p.X, p.Y, p.Y, p.A, p.A, p.B, p.B)
Output: p=(cast_test.MyStruct), X=3 (int), Y=4 (int), A=hello (string), B=world (string)
Example (MapToStructTags) ¶
type MyStruct struct {
X int `cast:"field_x"`
Y int `cast:"field_y"`
A string `cast:"field_a"`
B string `cast:"field_b"`
}
p := cast.To[MyStruct](map[string]string{
"field_x": "3", "field_y": "4", "field_a": "hello", "field_b": "world",
})
fmt.Printf("p=(%T), X=%v (%T), Y=%v (%T), A=%v (%T), B=%v (%T)",
p, p.X, p.X, p.Y, p.Y, p.A, p.A, p.B, p.B)
Output: p=(cast_test.MyStruct), X=3 (int), Y=4 (int), A=hello (string), B=world (string)
Example (NetIP) ¶
ip := cast.To[net.IP]("192.168.1.1")
fmt.Printf("%v", ip)
Output: 192.168.1.1
Example (NetIPFromUint32) ¶
// uint32 encodes a packed big-endian IPv4 address.
ip := cast.To[net.IP](uint32(0xC0A80101))
fmt.Printf("%v", ip)
Output: 192.168.1.1
Example (Regexp) ¶
re := cast.To[*regexp.Regexp](`^\d+$`)
fmt.Printf("%v %v", re.MatchString("42"), re.MatchString("hi"))
Output: true false
Example (Slice) ¶
v := cast.To[[]int]([]string{"1", "2", "3"})
fmt.Printf("%v (%T)", v, v)
Output: [1 2 3] ([]int)
Example (SliceUniqueValues) ¶
v := cast.To[[]int]([]int{1, 2, 1, 3}, cast.Op{cast.UNIQUE_VALUES, true})
fmt.Printf("%v (%T)", v, v)
Output: [1 2 3] ([]int)
Example (String) ¶
v := cast.To[string](1.234)
fmt.Printf("%#v (%T)", v, v)
Output: "1.234" (string)
Example (StringFromBytes) ¶
v := cast.To[string]([]byte("hello"))
fmt.Printf("%v", v)
Output: hello
Example (StringJson) ¶
v := cast.To[string](`hello "world"`, cast.Op{cast.JSON, true})
fmt.Printf("%v", v)
Output: "hello \"world\""
Example (StringerTarget) ¶
// Any source that already implements fmt.Stringer is returned as-is.
ip := net.ParseIP("127.0.0.1")
result := cast.To[fmt.Stringer](ip)
fmt.Printf("%v", result)
Output: 127.0.0.1
Example (Struct) ¶
type Point struct{ X, Y int }
p := cast.To[Point](map[string]any{"X": "3", "Y": "4"})
fmt.Printf("X=%v Y=%v (%T)", p.X, p.Y, p)
Output: X=3 Y=4 (cast_test.Point)
Example (StructNested) ¶
type Address struct{ City, Country string }
type Person struct {
Name string
Age int
Address Address
}
m := map[string]any{
"Name": "Alice",
"Age": "30",
"Address": map[string]any{
"City": "Portland",
"Country": "US",
},
}
p := cast.To[Person](m)
fmt.Printf("Name=%v Age=%v City=%v Country=%v", p.Name, p.Age, p.Address.City, p.Address.Country)
Output: Name=Alice Age=30 City=Portland Country=US
Example (StructToMap) ¶
type YourStruct struct {
X int
Y int
A string
B string
}
p := cast.To[map[string]string](YourStruct{X: 3, Y: 4, A: "hello", B: "world"})
fmt.Printf("p=(%T), X=%v (%T), Y=%v (%T), A=%v (%T), B=%v (%T)",
p, p["X"], p["X"], p["Y"], p["Y"], p["A"], p["A"], p["B"], p["B"])
Output: p=(map[string]string), X=3 (string), Y=4 (string), A=hello (string), B=world (string)
Example (StructToMapNonStrict) ¶
type YourStruct struct {
X int
Y int
A string
B string
}
// Non-strict: string fields A and B cannot convert to int and silently become 0.
p := cast.To[map[string]int](YourStruct{X: 3, Y: 4, A: "hello", B: "world"})
fmt.Printf("p=(%T), X=%v (%T), Y=%v (%T), A=%v (%T), B=%v (%T)",
p, p["X"], p["X"], p["Y"], p["Y"], p["A"], p["A"], p["B"], p["B"])
Output: p=(map[string]int), X=3 (int), Y=4 (int), A=0 (int), B=0 (int)
Example (StructToStruct) ¶
type MyStruct struct {
X int
Y int
A string
B string
}
type YourStruct struct {
X string
Y string
A string
B string
}
p := cast.To[MyStruct](YourStruct{X: "3", Y: "4", A: "hello", B: "world"})
fmt.Printf("p=(%T), X=%v (%T), Y=%v (%T), A=%v (%T), B=%v (%T)",
p, p.X, p.X, p.Y, p.Y, p.A, p.A, p.B, p.B)
Output: p=(cast_test.MyStruct), X=3 (int), Y=4 (int), A=hello (string), B=world (string)
Example (StructToStructJsonTags) ¶
type MyStruct struct {
X int `json:"fieldX"`
Y int `json:"fieldY"`
a string `json:"fieldA"`
b string `json:"fieldB"`
}
type YourStruct struct {
fieldX string
fieldY string
fieldA string
fieldB string
}
p := cast.To[MyStruct](YourStruct{fieldX: "3", fieldY: "4", fieldA: "hello", fieldB: "world"}, cast.Op{cast.PRIVATE, true})
fmt.Printf("p=(%T), X=%v (%T), Y=%v (%T), a=%v (%T), b=%v (%T)",
p, p.X, p.X, p.Y, p.Y, p.a, p.a, p.b, p.b)
Output: p=(cast_test.MyStruct), X=3 (int), Y=4 (int), a=hello (string), b=world (string)
Example (StructToStructPrivate) ¶
type MyStruct struct {
X int
Y int
a string
b string
}
type YourStruct struct {
X string
Y string
a string
b string
}
p := cast.To[MyStruct](YourStruct{X: "3", Y: "4", a: "hello", b: "world"}, cast.Op{cast.PRIVATE, true})
fmt.Printf("p=(%T), X=%v (%T), Y=%v (%T), a=%v (%T), b=%v (%T)",
p, p.X, p.X, p.Y, p.Y, p.a, p.a, p.b, p.b)
Output: p=(cast_test.MyStruct), X=3 (int), Y=4 (int), a=hello (string), b=world (string)
Example (Time) ¶
t := cast.To[time.Time]("2024-04-22T12:00:00Z")
fmt.Printf("%v", t.Format(time.RFC3339))
Output: 2024-04-22T12:00:00Z
Example (TimeFormat) ¶
// FORMAT overrides the default set of tried formats.
t := cast.To[time.Time]("2024/04/22", cast.Op{cast.FORMAT, "2006/01/02"})
fmt.Printf("%v", t.Format(time.DateOnly))
Output: 2024-04-22
Example (TimeFromFloat) ¶
// float64 input is Unix seconds; fractional seconds are preserved.
t := cast.To[time.Time](float64(1.5))
fmt.Printf("%v", t.Format(time.RFC3339Nano))
Output: 1970-01-01T00:00:01.5Z
Example (TimeFromInt) ¶
// int64 input is Unix nanoseconds.
t := cast.To[time.Time](int64(1_000_000_000))
fmt.Printf("%v", t.Format(time.RFC3339))
Output: 1970-01-01T00:00:01Z
Example (UintAbs) ¶
v := cast.To[uint]("-1", cast.Op{cast.ABS, true})
fmt.Printf("%v (%T)", v, v)
Output: 1 (uint)
Example (Url) ¶
u := cast.To[*url.URL]("https://example.com/path?q=1")
fmt.Printf("%v %v", u.Host, u.Path)
Output: example.com /path
func ToE ¶
ToE casts the value v to the given type, returning any errors.
ops is an optional variadic list of Op values that control conversion behavior. If omitted, the default conversion behavior for the target type is used. Available options depend on the target type; see the documentation for the specific type conversion function for more information.
Complex types have specific default behaviors, for example:
If the target type is a channel, a buffered channel of size 1 is created and the cast value `v` is sent to the channel before it is returned.
If the target type is a slice, a slice is created. To pre-allocate backing capacity set the LENGTH flag: `cast.ToE[[]int](v, cast.Op{cast.LENGTH, 10})`. The source must itself be a slice or array; scalar sources are rejected.
If the target type is a map, the source is converted into the target map type. Supported sources: map (key/value types cast), struct or *struct (field names become keys), slice or array (indices become keys).
See the documentation for the specific type conversion function for more information.
Example (BigFloat) ¶
_, e := cast.ToE[*big.Float]("not-a-float")
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (BigInt) ¶
_, e := cast.ToE[*big.Int]("not-a-number")
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (BoolErr) ¶
_, e := cast.ToE[bool]("maybe")
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (ChanErr) ¶
_, e := cast.ToE[chan int]("not-a-number")
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (Complex128) ¶
_, e := cast.ToE[complex128]("not-a-number")
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (DecodeErr) ¶
// DECODE=json with invalid JSON returns an error.
_, e := cast.ToE[[]int]("not json", cast.Op{cast.DECODE, "json"})
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (Duration) ¶
// A bare number without a unit suffix is not a valid duration string.
_, e := cast.ToE[time.Duration]("5")
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (ErrorTarget) ¶
// A source that does not implement error always fails.
_, e := cast.ToE[error](42)
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (ErrorWithDefault) ¶
v, e := cast.ToE[int]("Hi!", cast.Op{cast.DEFAULT, 10})
fmt.Printf("%#v (%T), %v", v, v, e)
Output: 10 (int), strconv.ParseFloat: parsing "Hi!": invalid syntax: strconv.ParseFloat: parsing "Hi!": invalid syntax: unable to cast "Hi!" of type string to int
Example (Float64) ¶
_, e := cast.ToE[float64]("not-a-float")
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (FuncErr) ¶
_, e := cast.ToE[cast.Func[int]]("not-a-number")
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (Int) ¶
_, e := cast.ToE[int]("not-a-number")
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (MapDuplicateKeyError) ¶
// After casting, "1" and "01" both become the integer key 1 — a duplicate.
m, e := cast.ToE[map[int]string](
map[string]string{"1": "one", "01": "also-one"},
cast.Op{cast.DUPLICATE_KEY_ERROR, true},
)
fmt.Printf("m=%v, err=%v", m, e != nil)
Output: m=map[], err=true
Example (MapErr) ¶
// A scalar source cannot be converted to a map.
_, e := cast.ToE[map[string]int](42)
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (NetIP) ¶
_, e := cast.ToE[net.IP]("not-an-ip")
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (Regexp) ¶
_, e := cast.ToE[*regexp.Regexp]("[unclosed")
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (SliceErr) ¶
// Elements that cannot convert to the target type cause an error.
_, e := cast.ToE[[]int]([]string{"a", "b"})
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (String) ¶
// A map containing a channel cannot be JSON-marshalled — string conversion fails.
_, e := cast.ToE[string](map[string]chan int{"a": make(chan int)})
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (StringerTarget) ¶
// A source that does not implement fmt.Stringer always fails.
_, e := cast.ToE[fmt.Stringer](42)
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (StructStrict) ¶
type Point struct{ X, Y int }
// "Z" has no matching field in Point — STRICT turns that into an error.
_, e := cast.ToE[Point](
map[string]any{"X": 1, "Y": 2, "Z": 3},
cast.Op{cast.STRICT, true},
)
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (Time) ¶
_, e := cast.ToE[time.Time]("not-a-time")
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (TimeFormat) ¶
// With FORMAT set, standard formats are not tried as a fallback.
// "2024-04-22" uses dashes but the custom format expects slashes.
_, e := cast.ToE[time.Time]("2024-04-22", cast.Op{cast.FORMAT, "2006/01/02"})
fmt.Printf("err=%v", e != nil)
Output: err=true
Example (UintErr) ¶
v, e := cast.ToE[uint]("-1")
fmt.Printf("%v (%T), %v", v, v, e)
Output: 0 (uint), cannot cast signed value to unsigned integer: unable to cast "-1" of type string to uint
Example (Url) ¶
// nil is the simplest source that always errors for *url.URL.
_, e := cast.ToE[*url.URL](nil)
fmt.Printf("err=%v", e != nil)
Output: err=true
Types ¶
type Func ¶
type Func[TTo Types] func() TTo
Func is a named zero-argument function type that returns a T. A named type is required because Go generics cannot use plain function literals as type parameters directly.
type Op ¶ added in v2.0.1
Op is a single key/value option passed to To or ToE. Build one with a Flag constant and the appropriate value type for that flag.
The Val type must exactly match the target type T for DEFAULT, or the converter returns an error immediately — before the input is even inspected.
type Tbase ¶
type Tbase interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64 |
~complex64 | ~complex128 |
~string | ~bool |
any
}
Tbase covers all scalar types. The any term makes this constraint effectively unconstrained — all types satisfy it. This is intentional: interface targets like error and fmt.Stringer need to be expressible as TTo, and there is no way to enumerate all interface types. Unsupported kinds are rejected at runtime by ToE's dispatch switch.
type Tchan ¶
type Tchan interface {
~chan Tbase |
~chan []int | ~chan []int8 | ~chan []int16 | ~chan []int32 | ~chan []int64 |
~chan []uint | ~chan []uint8 | ~chan []uint16 | ~chan []uint32 | ~chan []uint64 | ~chan []uintptr |
~chan []float32 | ~chan []float64 |
~chan []complex64 | ~chan []complex128 |
~chan []string | ~chan []bool |
~chan []any | ~chan Func[Tbase] |
~chan chan Tbase
}
Tchan covers channels of scalars (~chan Tbase covers all basic-type channels in one term), channels of slices, channels of Func values, and nested channels (chan chan T).
type Tmap ¶
type Tmap interface {
~map[Tbase]Tbase |
~map[Tbase][]int |
~map[Tbase][]int8 |
~map[Tbase][]int16 |
~map[Tbase][]int32 |
~map[Tbase][]int64 |
~map[Tbase][]uint |
~map[Tbase][]uint8 |
~map[Tbase][]uint16 |
~map[Tbase][]uint32 |
~map[Tbase][]uint64 |
~map[Tbase][]uintptr |
~map[Tbase][]float32 |
~map[Tbase][]float64 |
~map[Tbase][]complex64 |
~map[Tbase][]complex128 |
~map[Tbase][]string |
~map[Tbase][]bool |
~map[Tbase][]any |
~map[Tbase][]Func[Tbase]
}
Tmap covers map types whose keys are any Tbase type and whose values are either a scalar Tbase or a slice of scalars. Named types with a matching underlying map type (e.g. type Attrs map[string]any) also satisfy Tmap.
type Tslice ¶
type Tslice interface {
~[]int | ~[]int8 | ~[]int16 | ~[]int32 | ~[]int64 |
~[]uint | ~[]uint8 | ~[]uint16 | ~[]uint32 | ~[]uint64 | ~[]uintptr |
~[]float32 | ~[]float64 |
~[]complex64 | ~[]complex128 |
~[]string | ~[]bool |
~[]any
}
Tslice covers slice types of every scalar element kind, plus named types with the same underlying slice type (e.g. type Tags []string).
type Types ¶
type Types interface {
Tbase | Tslice | Tchan | Tmap | Func[Tbase] |
Func[[]int] | Func[[]int8] | Func[[]int16] | Func[[]int32] | Func[[]int64] |
Func[[]uint] | Func[[]uint8] | Func[[]uint16] | Func[[]uint32] | Func[[]uint64] | Func[[]uintptr] |
Func[[]float32] | Func[[]float64] |
Func[[]complex64] | Func[[]complex128] |
Func[[]string] | Func[[]bool] | Func[[]any] |
Func[chan int] | Func[chan int8] | Func[chan int16] | Func[chan int32] | Func[chan int64] |
Func[chan uint] | Func[chan uint8] | Func[chan uint16] | Func[chan uint32] | Func[chan uint64] | Func[chan uintptr] |
Func[chan float32] | Func[chan float64] |
Func[chan complex64] | Func[chan complex128] |
Func[chan string] | Func[chan bool] | Func[chan any]
}
Types is the top-level constraint that To and ToE accept as TTo. It unions all supported target categories. Func variants for slices and channels are enumerated explicitly because Go does not expand Func[Tslice] into all individual Func[[]T] terms automatically.
