Related to #221
For tag version v0.3.1
package main
import (
"bytes"
"log"
"github.com/BurntSushi/toml"
)
func demo0() {
type X struct {
Int int `toml:"int,omitempty"`
Str string `toml:"str,omitempty"`
F64 float64 `toml:"f64,omitempty"`
I64 int64 `toml:"i64,omitempty"`
U64 uint64 `toml:"u64,omitempty"`
}
x := X{Str: ""}
var buf bytes.Buffer
if err := toml.NewEncoder(&buf).Encode(x); err != nil {
log.Fatal(err)
}
log.Printf("\n%s", buf.String())
}
func main() {
demo0()
}
the output is
2021/08/01 13:12:06
int = 0
f64 = 0.0
i64 = 0
u64 = 0
It seems that, the omitempty
not working on int and float.
You need to use omitzero
, not omitempty
. The way it works:
omitempty
– Don't write if the value is "empty": len(0) for arrays, slices, maps, strings, false for bools
omitzero
– Don't write if the value is zero, only works for numbers.
It's annoying that it works like this, but there isn't any way to change this to the more standard behaviour without breaking comparability. It's one of the things that will be fixed in a hypothetical unplanned v2 release. See: #120.
Owner Name | BurntSushi |
Repo Name | toml |
Full Name | BurntSushi/toml |
Language | Go |
Created Date | 2013-02-26 |
Updated Date | 2023-03-20 |
Star Count | 4154 |
Watcher Count | 84 |
Fork Count | 518 |
Issue Count | 15 |