Skip to content

Manipulates(gets,sets,convert etc.) interface{} value, convenient and simple, and reflect-based.

License

Notifications You must be signed in to change notification settings

helloyi/go-value

Repository files navigation

The Value in go

GoDoc Build Status Coverage Status Go Report Card stability-unstable License

Manipulates(gets,sets,convert etc.) interface{} value, convenient and simple, and reflect-based.

Features

You can do the following things, after new Value from interface{} like this v := value.New(var).

  • Get value from map slice array or struct with key idx or fieldname
  • Put key value pair to map slice or array
  • Set value
  • For range value any type
  • Get value with Int*, Float*, PList etc.
  • Numerical type is adaptive when get; example, i kind is uint8 and get with Int16() is legal, the value is automatically converted to int16 instead of type mismatch.
  • Provide Must* API, for chaining call and some friendly writing.
  • Unmarshal to a value with Value.ConvTo
  • Supported covert to time.Duration or time.Time etc..
  • Supported convert to frequently used build-in type

Example

example_test.go

convert v to net.IP, time.Duration etc..

package main

import (
	"fmt"
	"log"
	"net"
	"time"

	"github.com/helloyi/go-value"
)

func main() {
	x := map[string]interface{}{
		"a": 1,
		"B": "b",
		"C": "c",
		"D": "13s",
		"E": "Fri Nov 1 19:13:55 +0800 CST 2019",
		"F": "8.8.8.8",
	}

	var y struct {
		A int    `value:"a"` // set with name "a"
		B int    `value:"_"` // passed
		C string // set with name "C"
		D time.Duration
		E *time.Time
		F *net.IP
	}

	v := value.New(x)
	if err := v.ConvTo(&y); err != nil {
		log.Fatalln(err)
	}
	fmt.Printf("%v\n", y)
	// Output: {1 0 c 13s 2019-11-01 19:13:55 +0800 CST 8.8.8.8}
}

About

Manipulates(gets,sets,convert etc.) interface{} value, convenient and simple, and reflect-based.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages