omitempty not working for int/float

This issue has been created since 2021-08-01.

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.

arp242 wrote this answer on 2021-08-01

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.

More Details About Repo
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

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
console program name `wd` is used also by wandb 0 2022-11-25 2023-02-27
在屏幕低亮度的条件下,蓝色字体容易看不清 2 2022-03-20 2022-12-04
can you change the size of icons? 1 2022-12-23 2023-03-10
Calendar blocks in non-Atomik themes don't display calendar due to the lack of moment.js 2 2022-08-26 2023-02-21
Add more options for "after each login output" 1 2021-08-02 2023-02-16
es module, for modern browser use without bundlers 2 2019-08-20 2023-02-26
[BUG] PDFs not consumed via "consume" directory 5 2022-08-31 2023-02-26
Question: possible to connect multiple outputs to single input interface? 2 2021-02-03 2023-03-13
[Enhancement]: Author metadata in OPF 0 2022-05-23 2022-11-04
Supporting Apple Silicon 3 2022-02-22 2023-02-07
Unable to install openJdk8 on the container 9 2018-05-09 2023-02-05
Running other CLI arguments? 1 2017-05-29 2023-02-09
Unable limit labels to selected residues 2 2021-10-31 2023-03-01
Microtool to debug refcounting errors 0 2020-12-09 2023-03-02
How to bypass the 1080x1080 thing ? 0 2022-02-22 2023-02-15
2.0: release what you have now? 4 2022-10-11 2023-03-20
crash in rec mode with midi device(macOS 11.4) 5 2021-07-22 2022-12-10
AmbientCapabilities in systemd units is more privileged than Hashicorp's unit 0 2022-05-19 2023-03-12
Decrediton stop responding after a while of being minimized (1.7 RC2 on macOS) 2 2022-01-15 2023-01-31
Can't set individual agenda vote choices 0 2022-01-13 2023-02-15
@return never doesn't affect type inference if a function is reanalyzed and AddNeverReturnTypePlugin is enabled 4 2021-09-08 2023-02-18
Don't wipe all query stages when changing the source table 0 2023-02-27 2023-03-01
Stable selectors for the frontend 0 2023-02-27 2023-03-01
Helmfile go client 0 2021-07-21 2021-12-21
Feature request: `Async` codec 0 2022-08-28 2023-03-07
Promise.all ? 0 2021-02-14 2022-01-11
Color Specific Cell If The Text Matches 2 2022-09-02 2022-11-28
Fix docs links in v3 blog post 0 2022-01-13 2023-03-21
Fixes and improvements 0 2019-07-25 2022-01-14
Save Preview 2D and 3D configuration? 1 2021-07-05 2023-02-15
ActionController::RoutingError (No route matches [POST] "/auth/google_oauth2/callback") 4 2017-01-31 2023-03-22
Redesign toolbar 0 2021-12-15 2022-01-23
[RFC] Improvements to the Scaffolder Publish Action for Setting Author and Commit Message 2 2022-02-22 2023-02-10
Bloom filter flaky test 2 2022-01-22 2023-02-07
Create a high-performance async interface 5 2020-11-04 2023-03-20
FBW A320Neo Livery Paint Kit 1 2022-06-09 2023-02-14
blank screen after installing debugbar 1 2022-05-06 2023-03-12
Fetch errors uncaught 1 2022-05-05 2023-03-12
How to always show (relative) path to file? 4 2021-09-08 2023-02-13
EasyExcel表头自定义颜色不生效 1 2021-11-10 2022-01-20
`types-python-dateutil` Add return type for `rrule.__iter__`? 2 2022-08-26 2023-02-11
Applicability of rdtools.clip_filter in a degradation analysis that utilizes both NSRDB and PVDAQ data 1 2018-09-27 2023-03-22
`gradlew tasks` requires you to run `gradlew applyPatches` before it can run. 0 2021-08-23 2023-03-02
Fping exited unexpectedly with status 256 on podman 3 2022-12-27 2023-03-13
ItemsRepeater applying weird arranging behaviour to content when parent's ScrollViewer "HorizontalScrollBarVisibility" is set to "Visible". 13 2021-09-26 2023-02-18
docker swarm not installing 3 2019-04-28 2023-02-25
Sticky search bar for Bootstrap icons 0 2023-03-01 2023-03-14
expose the messages properties 0 2021-08-09 2023-02-20
Auditd Manager Integration 1 2022-04-06 2023-02-21
Add an option to ignore "public" folder during build and dev process 2 2022-04-05 2023-02-19