mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 01:02:28 +08:00
refactor: move typemap.go to maputil package and add document for it
This commit is contained in:
47
README.md
47
README.md
@@ -598,6 +598,8 @@ import "github.com/duke-git/lancet/v2/maputil"
|
||||
|
||||
#### Function list:
|
||||
|
||||
- **<big>MapTo</big>** : quick map any value to struct or any base type.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#MapTo)]
|
||||
- **<big>ForEach</big>** : executes iteratee funcation for every key and value pair in map.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ForEach)]
|
||||
[[play](https://go.dev/play/p/OaThj6iNVXK)]
|
||||
@@ -1340,51 +1342,6 @@ import "github.com/duke-git/lancet/v2/xerror"
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#TryUnwrap)]
|
||||
[[play](https://go.dev/play/p/acyZVkNZEeW)]
|
||||
|
||||
### 22. quick map any to struct or any base type
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/typemap"
|
||||
```
|
||||
#### Example
|
||||
```go
|
||||
type (
|
||||
Person struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
Phone string `json:"phone"`
|
||||
Addr Address `json:"address"`
|
||||
}
|
||||
|
||||
Address struct {
|
||||
Street string `json:"street"`
|
||||
Number int `json:"number"`
|
||||
}
|
||||
)
|
||||
|
||||
func main() {
|
||||
v := map[string]interface{}{
|
||||
"person":map[string]interface{}{
|
||||
"name":"Nothin",
|
||||
"age":123,
|
||||
"phone":"123421312",
|
||||
"address":map[string]interface{}{
|
||||
"street":"test",
|
||||
"number":1,
|
||||
},
|
||||
},
|
||||
"other":1234,
|
||||
}
|
||||
|
||||
var person Person
|
||||
err :=typemap.MapTo(v["person"],&person)
|
||||
if err != nil {
|
||||
//error handler ...
|
||||
}
|
||||
|
||||
log.Println(person)
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
## How to Contribute
|
||||
|
||||
Reference in New Issue
Block a user