https://github.com/kitstack/structkit
StructKit is a simple tool that allows you to copy specific fields from a new struct and retrieve values from a struct or slice
https://github.com/kitstack/structkit
copy coverage extractor get go golang golang-library simple struct tag
Last synced: 2 months ago
JSON representation
StructKit is a simple tool that allows you to copy specific fields from a new struct and retrieve values from a struct or slice
- Host: GitHub
- URL: https://github.com/kitstack/structkit
- Owner: kitstack
- License: mit
- Created: 2022-11-15T02:50:23.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-20T02:08:04.000Z (about 3 years ago)
- Last Synced: 2026-01-14T17:47:31.032Z (5 months ago)
- Topics: copy, coverage, extractor, get, go, golang, golang-library, simple, struct, tag
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/kitstack/structkit/actions/workflows/coverage.yml)

[](https://goreportcard.com/report/github.com/kitstack/structkit)
[](https://codecov.io/gh/kitstack/structkit)
[](https://github.com/kitstack/structkit/blob/main/LICENSE)
[](https://github.com/kitstack/structkit/releases)
# 🚀 Overview
StructKit is simple tool for :
- [x] Copy specific fields a new struct.
- Tag copy
- [x] Get value of specific field.
- Slice
- Struct
- Pointer
- [x] Set value of specific field.
- Slice (Append, Replace or Update)
- Struct
- Pointer
- [x] Coverage
- 100% tested code 🤓
- [x] Benchmark
- Get (Optimized with cache)
### Copy
```go
package main
import (
"github.com/kitstack/structkit"
"log"
)
type Foo struct {
Counter int `json:"int"`
Value string `json:"value"`
Struct Bar `json:"struct"`
Slice []Bar `json:"Slice"`
}
type Bar struct {
Value string `json:"value"`
}
func main() {
payload := Foo{Value: "foo", Struct: Bar{Value: "bar"}}
log.Printf("%v", structkit.Copy(payload, "Value", "Struct.Value")) // {foo {bar}}
}
```
### Get
```go
package main
import (
"github.com/kitstack/structkit"
"log"
)
type Foo struct {
Counter int `json:"int"`
Value string `json:"value"`
Struct Bar `json:"struct"`
Slice []Bar `json:"Slice"`
}
type Bar struct {
Value string `json:"value"`
}
func main() {
payload := Foo{Value: "foo", Struct: Bar{Value: "bar"}, Slice: []Bar{{Value: "baz"}}}
log.Printf("%v", structkit.Get(payload, "Value")) // foo
log.Printf("%v", structkit.Get(payload, "Struct/Value", &structkit.Option{Delimiter: "/"})) // bar
log.Printf("%v", structkit.Get(payload, "Slice.[0].Value")) // baz
}
```
### Set
```go
package main
import (
"github.com/kitstack/structkit"
"log"
)
type Foo struct {
Value string
Struct Bar
StructP *Bar
Slice []Bar
}
type Bar struct {
Value string
}
func main() {
payload := Foo{}
err := structkit.Set(&payload, "Value", "foo")
if err != nil {
panic(err)
}
log.Print(payload.Value) // foo
err = structkit.Set(&payload, "Struct.Value", "bar")
if err != nil {
panic(err)
}
log.Print(payload.Struct.Value) // bar
err = structkit.Set(&payload, "StructP.Value", "bar")
if err != nil {
panic(err)
}
log.Print(payload.StructP.Value) // bar
err = structkit.Set(&payload, "Slice.[*]", Bar{Value: "bar"})
if err != nil {
panic(err)
}
log.Print(payload.Slice) // [{bar}]
err = structkit.Set(&payload, "Slice.[0].Value", "bar updated")
if err != nil {
panic(err)
}
log.Print(payload.Slice) // [{bar updated}]
}
```
### 💪 Benchmark
```bash
goos: darwin
goarch: arm64
pkg: github.com/kitstack/structkit
BenchmarkGet
BenchmarkGet-10 6346312 194.0 ns/op
BenchmarkGetEmbeddedValue
BenchmarkGetEmbeddedValue-10 5543814 209.5 ns/op
BenchmarkGetEmbeddedSliceValue
BenchmarkGetEmbeddedSliceValue-10 4526838 262.4 ns/op
```
## 🤝 Contributions
Contributors to the package are encouraged to help improve the code.