zreflect

package
Version: v0.0.0-...-6fe5ffd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2021 License: MIT Imports: 1 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Simplify

func Simplify(value interface{}) (reflect.Value, reflect.Kind)

Simplify a type.

This reduces the Go types a limited set of values, making type switches a bit easier.

everything else       No conversation, return as-is.
bool                  reflect.Bool
complex*              reflect.Complex128
int*, uint*, float*   reflect.Float64; as floats are reliable for natural
                      numbers up to ~9e15 (9,007,199 billion) just
                      converting it to a float will be fine in most use
                      cases.
string, []byte        reflect.String; note this also matches []uint8 and []uint32,
        []rune        as byte and rune are just aliases for that with no way to
                      distinguish between the two.

The practical value of this is that it makes it a lot easier to deal with different types:

switch vv, t := Simplify(v); t {
case reflect.String:
    ..
case reflect.Float64:
    ..
case reflect.Bool:
    ..
case reflect.Complex128:
    ..

default:
    if vv.Type() != reflect.TypeOf(time.Time{}) {
        return vv.String()
    }
}

Types

This section is empty.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL