A modern Go library for struct utilities — convert structs to maps, extract field names and values, inspect and mutate fields. Drop-in replacement for github.com/fatih/structs.
go get github.com/agentine/structifypackage main
import (
"fmt"
"github.com/agentine/structify"
)
type Server struct {
Name string `json:"name"`
ID int `json:"id"`
Enabled bool `json:"enabled"`
}
func main() {
s := Server{Name: "web", ID: 1, Enabled: true}
// Convert struct to map
m := structify.Map(s)
fmt.Println(m) // map[Name:web ID:1 Enabled:true]
// Get field names
names := structify.Names(s)
fmt.Println(names) // [Name ID Enabled]
// Get field values
values := structify.Values(s)
fmt.Println(values) // [web 1 true]
// Use custom tag for map keys
st := structify.New(s, structify.WithTag("json"))
m = st.Map()
fmt.Println(m) // map[name:web id:1 enabled:true]
}| Function | Description |
|---|---|
New(s any, opts ...Option) *Struct |
Wrap a struct value |
Map(s any) map[string]any |
Convert struct to map |
Values(s any) []any |
Extract field values |
Names(s any) []string |
Extract field names |
Fields(s any) []*Field |
Get all fields |
IsZero(s any) bool |
Check if all fields are zero |
HasZero(s any) bool |
Check if any field is zero |
IsStruct(s any) bool |
Check if value is a struct |
Name(s any) string |
Get struct type name |
| Method | Description |
|---|---|
Name() string |
Field name |
Value() any |
Field value |
Kind() reflect.Kind |
Field kind |
Tag(key string) string |
Get struct tag value |
IsZero() bool |
Check if zero value |
IsExported() bool |
Check if exported |
IsEmbedded() bool |
Check if embedded |
Set(val any) error |
Set field value |
Fields() []*Field |
Nested struct fields |
| Option | Description |
|---|---|
WithTag(tag string) |
Use custom struct tag for map keys (default: field name) |
structify is a drop-in replacement for fatih/structs. Key differences:
anyinstead ofinterface{}— Modern Go style throughout.- Error returns —
Struct.Field(name)returns(*Field, error)instead of panicking. - Configurable tags —
New(s, WithTag("json"))to use any struct tag for map keys. - Nil pointer safety — Graceful handling of nil embedded pointers.
- import "github.com/fatih/structs"
+ import "github.com/agentine/structify"
- m := structs.Map(s)
+ m := structify.Map(s)
- f := structs.Fields(s)
+ f := structify.Fields(s)The compatibility layer provides FillMap and other functions matching the original API signatures.
MIT