Skip to content

Add omitzero tag. #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2025
Merged

Add omitzero tag. #98

merged 2 commits into from
Jun 6, 2025

Conversation

Crystalix007
Copy link

Adds an omitzero tag to allow "zero values" according to the Go language spec to be ignored.

The important distinction between this and omitempty is for slices and maps - where currently there is no good way to make the following code sample do something different depending upon whether the opt1 option is specified (as a zero-length slice) or nil:

type options struct {
	opt1 []string
}

func foo(opts options) {
	// ...
}
Improves the documentation of the existing `omitempty` struct tag, as it
relates to nil and empty slices.
Adds a tag for omitting zero-valued elements, such as nil slices and
maps. This enables a bit more fine-grained control over whether a slice
will be copied across (by explicitly choosing to set as nil or
zero-length).
@sagikazarmark sagikazarmark added the enhancement New feature or request label Jun 6, 2025
Copy link
Member

@sagikazarmark sagikazarmark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome!

Thanks a lot!

@sagikazarmark sagikazarmark merged commit 1a66224 into go-viper:main Jun 6, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
2 participants