Struct kits for Golang development.
go get -u github.com/likexian/gokit
import (
"github.com/likexian/gokit/xstruct"
)
Visit the docs on GoDoc
// 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}
// ["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")
// 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")
Copyright 2012-2024 Li Kexian
Licensed under the Apache License 2.0
If this project is helpful, please share it with friends.
If you want to thank me, you can give me a cup of coffee.