File: README.md

package info (click to toggle)
golang-github-likexian-gokit 0.24.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 720 kB
  • sloc: makefile: 5
file content (89 lines) | stat: -rw-r--r-- 1,574 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# GoKit - xstruct

Struct kits for Golang development.

## Installation

    go get -u github.com/likexian/gokit

## Importing

    import (
        "github.com/likexian/gokit/xstruct"
    )

## Documentation

Visit the docs on [GoDoc](https://godoc.org/github.com/likexian/gokit/xstruct)

## Example

### Define a struct first

```go
// Define Staff struct
type Staff struct {
    Id int64 `json:"id"`
    Name string  `json:"name"`
    Enabled bool  `json:"enabled"`
}

// Init staff struct
staff := Staff{1, "likexian", true}
```

### Use as global functions

```go
// ["Id", "Name", "Enabled"]
names, _ := xstruct.Names(staff)

// [1, "likexian", true]
values, _ := xstruct.Values(staff)

// list all field as [*Field]
fields, _ := xstruct.Fields(staff)

// get struct field value
value, _ := xstruct.Field(staff, "Name").Value()

// set struct field value
xstruct.Set(staff, "Name", "kexian.li")
```

### Use as Interactive mode

```go
// create a xstruct object
s, err := xstruct.New(staff)
if err != nil {
    panic(err)
}

// ["Id", "Name", "Enabled"]
names := s.Names()

// [1, "likexian", true]
values := s.Values()

// list all field as [*Field]
fields := s.Fields()

// get struct field value
value := s.Field("Name").Value()

// set struct field value
s.Set("Name", "kexian.li")
```

## License

Copyright 2012-2021 [Li Kexian](https://www.likexian.com/)

Licensed under the Apache License 2.0

## Donation

If this project is helpful, please share it with friends.

If you want to thank me, you can [give me a cup of coffee](https://www.likexian.com/donate/).