Writing Idiomatic Go Structs for encoding/json
Go's encoding/json package only marshals and unmarshals exported (capitalized) struct fields, relying on `json:"snake_case"` struct tags to map wire keys to idiomatic Go identifiers. Following gofmt and staticcheck rules, common initialisms such as ID, URL, IP, HTTP, and API stay uppercase (e.g., UserID, APIURL) rather than mixed-case.
Be deliberate when adding `,omitempty` to boolean or integer fields: encoding/json treats zero values (`false`, `0`, `""`) as empty and omits them during marshaling, which can accidentally drop explicit `false` flags from PATCH requests. For nullable JSON fields where distinguishing null from zero matters, use pointer types such as `*string` or `*int64`.
- Go Naming ConventionsApplies Effective Go initialisms (ID, URL, HTTP, IP, JSON, SQL, API, UUID) and exported PascalCase fields
- Numeric & Time MappingMaps integers to int64/int, decimals to float64, ISO-8601 strings to time.Time, and nulls to pointer/any
- Struct LayoutSupports separate reusable named structs or compact inline nested struct definitions with optional ,omitempty tags
Why does the generator map mixed int and float arrays to []float64?
Go slices require a single static element type. When a JSON array contains both 21 and 21.5, []float64 is the narrowest numeric type that avoids unmarshal errors.
When should I avoid using ,omitempty in Go struct tags?
Avoid ,omitempty on non-pointer bool or int fields if 0 or false is a meaningful value that must be serialized into outgoing JSON.
How are RFC 3339 timestamps mapped in Go?
Strings matching ISO-8601 / RFC 3339 format map cleanly to time.Time, which implements json.Marshaler and json.Unmarshaler in Go's standard library.