New Feature in Go 1.24: The omitzero Struct Field Tag ??

New Feature in Go 1.24: The omitzero Struct Field Tag ??

Go 1.24, released in February 2025, brings an exciting new feature to the language's encoding/json package — the omitzero option for struct field tags! This addition enhances the flexibility and clarity of JSON marshaling, especially when dealing with zero values.

In this article, we'll dive into:

  • How the omitzero tag works ??
  • Why it's clearer than omitempty ??
  • Practical use cases with examples ??

Why omitzero?

In Go, JSON marshaling is a frequent task when building APIs or communicating with external services. Previously, developers relied on the omitempty option to omit fields with "empty" values, such as zero-length slices or nil pointers. However, the omitempty option could be ambiguous and lead to unexpected behavior, especially with fields like time.Time or types that implement their own "zero" definition.

With the introduction of the omitzero tag, developers now have a more explicit way to omit zero values during JSON marshaling. The omitzero tag ensures that fields with zero values (or custom-defined zero values) are omitted, streamlining JSON outputs and preventing unwanted data from being sent or stored.

How omitzero Works ??

The omitzero tag works similarly to omitempty, but with a key difference: it omits fields specifically when their values are zero. The zero value for each type in Go is clearly defined:

int, float, and other number types: 0

bool: false

string: "" (empty string)

Pointers, maps, slices: nil

struct fields: Their zero value, unless otherwise specified

Moreover, if the field type defines an "IsZero() bool" method, Go will use this method to determine whether the value is zero.

Here’s a quick example using omitempty:

Using omitempty

And then using omitzero:

Using omitzero

In this example, the Age field is omitted from the JSON output because its value is zero (0). Similarly, the CreatedAt field is omitted because its value is the zero value of time.Time.

IsZero() Method for Custom Types ???

For custom types, you can define your own IsZero() method to control how the omitzero tag behaves. If your type implements this method, Go will call it during marshaling to decide whether to omit the field.

In this case, any instance of CustomType with a value of 99 will be considered zero and omitted from the JSON output.

Handling Both omitempty and omitzero Together ???

Interestingly, Go allows you to use both omitzero and omitempty in the same field tag. If both are specified, the field will be omitted if the value is either empty or zero. This gives you even more granular control over your JSON output.

Key Takeaways ??

omitzero omits fields when their values are zero, providing a more explicit alternative to omitempty.

You can define custom zero-value logic by implementing the IsZero() method on your types.

omitzero solves common issues, especially when dealing with types like time.Time, which are problematic with omitempty.

You can combine both omitzero and omitempty in a single tag to handle both zero and empty values in your JSON output.


With omitzero in Go 1.24, developers now have a more precise tool for controlling JSON marshaling behavior. This enhancement not only simplifies your code but also makes it easier to produce clean, predictable JSON outputs.

Kostiantyn K.

???? Senior Frontend Developer | Software Engineer

2 周

Thanks for sharing this article! I noticed a small mistake - when using "omitempty" for the age field, it will indeed be omitted if its value is 0. While "omitempty" and "omitzero" are similar, "omitzero" has a few key advantages: 1. It is more reliable than "omitempty", especially for omitting zero-valued time.Time fields 2. If a field type has an IsZero() bool method, "omitzero" will use it to determine whether the value should be omitted.

Gourav Ahuja

Senior Developer at Barclays

2 周

Hey Auber Mardegan , shouldn’t the age be omitted in omitempty example’s commented output? omitempty omits int fields when 0.

回复
Jardel Moraes

Data Engineer | Python | SQL | PySpark | Databricks | Azure Certified: 5x

3 周

Love your take on this—thank you! ??

Patrick Cunha

Lead Fullstack Engineer | Typescript Software Engineer | Nestjs | Nodejs | Reactjs | AWS

3 周

This is a great overview of the new feature! The examples are particularly helpful for understanding the practical applications. Looking forward to exploring this further.

Dr. Yurii Kolochkov

Golang & Cloud-Native Solutions

3 周

Nice one

要查看或添加评论,请登录

Auber Mardegan的更多文章

社区洞察

其他会员也浏览了