https://github.com/helloyi/go-value
Manipulates(gets,sets,convert etc.) interface{} value, convenient and simple, and reflect-based.
https://github.com/helloyi/go-value
configuration go golang mapstructure viper
Last synced: 4 months ago
JSON representation
Manipulates(gets,sets,convert etc.) interface{} value, convenient and simple, and reflect-based.
- Host: GitHub
- URL: https://github.com/helloyi/go-value
- Owner: helloyi
- License: mit
- Created: 2019-10-31T05:13:58.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-06-23T09:12:54.000Z (almost 5 years ago)
- Last Synced: 2024-06-20T03:45:47.095Z (almost 2 years ago)
- Topics: configuration, go, golang, mapstructure, viper
- Language: Go
- Homepage:
- Size: 50.8 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# The Value in go
[](https://godoc.org/github.com/helloyi/go-value) [](https://travis-ci.com/helloyi/go-value) [](https://coveralls.io/github/helloyi/go-value?branch=master) [](https://goreportcard.com/report/github.com/helloyi/go-value)  [](https://github.com/helloyi/go-value/blob/master/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](https://github.com/helloyi/go-value/blob/master/example_test.go)
convert v to net.IP, time.Duration etc..
```golang
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}
}
```