Package any
is an empty interface replacement.
go get github.com/norunners/any
ValueOf
creates a Value
from any value.
v := any.ValueOf("hello")
fmt.Println(v.String())
// "hello"
Ok
determines whether Value
is not the zero Value
.
v := any.ValueOf("hello")
fmt.Println(v.Ok())
// true
[Type]Ok returns the value as a [Type] type and a bool whether the Value is of type [Type].
v := any.ValueOf("hello")
s, ok := v.StringOk()
fmt.Println(s, ok)
// "hello" true
Or
sets the Value to the default when not Ok
and returns the Value
.
var v any.Value
v = v.Or("world")
fmt.Println(v.String())
// "world"
[Type]Or returns the value as a [Type] type or the default when the Value is not of type [Type].
var v any.Value
s := v.StringOr("world")
fmt.Println(s)
// "world"
Set
assigns Value
to a non-nil pointer.
v := any.ValueOf(pair{q: "meaning", a: 42})
p := pair{}
if err := v.Set(&p); err != nil {
// Handle error.
}
fmt.Println(p)
// {"meaning", 42}
Interface
provides the underlying value as an empty interface.
v := any.ValueOf(pair{q: "meaning", a: 42})
if p, ok := v.Interface().(pair); ok {
fmt.Println(p)
// {"meaning", 42}
}
Map
represents a map of any values.
m := any.Map{"meaning": any.ValueOf(42)}
v := m["meaning"]
fmt.Println(v.Int())
// 42
MapOf
makes a Map
from a map of any values.
m := any.MapOf(map[string]interface{}{"meaning": 42})
v := m["meaning"]
fmt.Println(v.Int())
// 42
Put
puts the key value into the returned Map
.
var m any.Map
m = m.Put("meaning", 42)
v := m["meaning"]
fmt.Println(v.Int())
// 42
Equal
determines if a Value
is equal to another Value
.
v := any.ValueOf("hello")
fmt.Println(v.Equal(v))
// true
i := any.ValueOf(42)
fmt.Println(v.Equal(i))
// false
Map
also provides Equal
behavior.