Giter VIP home page Giter VIP logo

any's Introduction

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

any's People

Contributors

norunners avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.