Package cast provides tools for safely converting from one type to another, without information loss, for the Go programming language.
Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-cast
For an example, let's look at converting to an int64
.
You can safely convert a uint8
, uint16
, uint32
, plus an int
, int8
, int16
, int32
, int64
,
into an int64
without information loss.
In other words, if v
(in the following example code) is any of the types in the list just mentioned, then the following
code would successfully return an int64
of the same numeric value.
i64, err := cast.Int64(v)
There is something similar for the other Go built-in types. I.e.,:
b, err := cast.Bool(v)
c64, err := cast.Complex64(v)
c128, err := cast.Complex128(v)
f32, err := cast.Float32(v)
f64, err := cast.Float64(v)
i, err := cast.Int(v)
i8, err := cast.Int8(v)
i16, err := cast.Int16(v)
i32, err := cast.Int32(v)
i64, err := cast.Int64(v)
s, err := cast.String(v)
t, err := cast.Time(v)
u, err := cast.Uint(v)
u8, err := cast.Uint8(v)
u16, err := cast.Uint16(v)
u32, err := cast.Uint32(v)
u64, err := cast.Uint64(v)
In addition to supporting those Go built-in types, this package also has tools for casting with common vector types:
// [2]float64
fv2, err := cast.Float64x2(v)
// [3]float64
fv3, err := cast.Float64x3(v)
// [4]float64
fv4, err := cast.Float64x4(v)
This package also supports converting from custom types, as long as that custom type implement a special method.
For int64, the special method is:
interface {
Int64() (int64, error)
}
For example:
type Always5 struct{}
func (receiver Always5) Int64() (int64, error) {
return 5, nil
}
// ...
var v Always5
// ...
i64, err := cast.Int64(v)
The other special methods are:
interface {
Bool() (bool, error)
}
interface {
Complex64() (complex64, error)
}
interface {
Complex128() (complex128, error)
}
interface {
Float32() (float32, error)
}
interface {
Float64() (float64, error)
}
interface {
Int() (int, error)
}
interface {
Int8() (int8, error)
}
interface {
Int16() (int16, error)
}
interface {
Int32() (int32, error)
}
interface {
Int64() (int64, error)
}
interface {
String() (String, error)
}
interface {
Time() (time.Time, error)
}
interface {
Uint() (uint, error)
}
interface {
Uint8() (uint8, error)
}
interface {
Uint16() (uint16, error)
}
interface {
Uint32() (uint32, error)
}
interface {
Uint64() (uint64, error)
}
For converting to a Go string
, the special method for converting from a custom type is:
interface {
String() (String, error)
}
This potentially conflicts with the "fmt"
package's fmt.Stringer
interface:
package fmt
// ...
type Stringer interface {
String() string
}
(Because these methods have the same name, a type cannot have both.)
To deal with this, this package will use either of these, when trying to convert from a custom type to a string
.
So both "String() (String, error)"
and "String() string"
are acceptable, to this package, for casting from a custom type to a Go string
.