Assisted-by: GLM 5.3 Flash
This commit is contained in:
+13
-5
@@ -475,22 +475,30 @@ nil map emits nothing.
|
||||
|
||||
### Tag options
|
||||
|
||||
The part of a `toml` tag after the first comma carries options. Both options
|
||||
shape emission only; the decoder ignores them.
|
||||
The part of a `toml` tag after the first comma carries options. They shape
|
||||
emission only; the decoder ignores them, so a value that round-trips keeps
|
||||
its key whether the table it came from was written inline or under a header.
|
||||
|
||||
- `omitzero` skips the field when its value is the zero value of its type. A
|
||||
type with an `IsZero() bool` method (time.Time among them) decides through
|
||||
that method, so a zero `time.Time` or an all-zero struct disappears from
|
||||
the output.
|
||||
- `omitempty` skips the field when it holds an empty collection: a nil or
|
||||
empty slice or array, or a nil or empty map. Strings and other scalars are
|
||||
not covered by `omitempty`; use `omitzero` for those.
|
||||
- `omitempty` skips the field when it holds an empty value in the
|
||||
encoding/json sense: an empty string, a zero number, `false`, a nil pointer
|
||||
or interface, and a nil or empty slice, array or map. This is a change of
|
||||
semantics against 1.x, where only collections were covered.
|
||||
- `inline` forces a struct or map field to emit as `name = {…}`, the inline
|
||||
table form, instead of a header section, whatever its size; a named
|
||||
embedded struct tagged this way does the same. A field holding an array of
|
||||
tables is an error under `inline`, because the inline form would re-parse
|
||||
as a value array and change the value's Go type.
|
||||
|
||||
```go
|
||||
type Config struct {
|
||||
Host string `toml:"host,omitzero"`
|
||||
Started time.Time `toml:"started,omitzero"`
|
||||
Tags []string `toml:"tags,omitempty"`
|
||||
Retry Retry `toml:"retry,inline"`
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user