Skip to content
/ any Public

Package any is an empty interface replacement.

License

Notifications You must be signed in to change notification settings

norunners/any

Repository files navigation

any

Go Reference

Package any is an empty interface replacement.

Install

go get github.com/norunners/any

Examples

ValueOf

ValueOf creates a Value from any value.

v := any.ValueOf("hello")
fmt.Println(v.String())
// "hello"

Ok

Ok determines whether Value is not the zero Value.

v := any.ValueOf("hello")
fmt.Println(v.Ok())
// true

[Type]Ok

[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

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

[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

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

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

Map represents a map of any values.

m := any.Map{"meaning": any.ValueOf(42)}
v := m["meaning"]
fmt.Println(v.Int())
// 42

MapOf

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

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

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.

License

About

Package any is an empty interface replacement.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages