Skip to content

encoding/json: Proposal - Flexible omitempty #45787

Closed
@PumpkinSeed

Description

@PumpkinSeed

Questions unrelated.

I run into the issue where I wanted to omitempty a non-pointer struct. I couldn't make it happen properly so I think about a solution. So this is the code running I guess when there is the omitempty tag on the field. What if it would check for an extra interface implementation where we can flexibly determine when we consider a non-pointer struct as empty?

So use-case in this playground. It seems that the empty struct marshalled into the JSON. But if the Go has an interface like below I can implement this and the linked source can look for that implementation. If no any implementation than go with the default.

type Something interface {
  IsEmpty() bool
}

Activity

ianlancetaylor

ianlancetaylor commented on Apr 26, 2021

@ianlancetaylor
Contributor

Dup of #32675, see discussion at #11939.

locked and limited conversation to collaborators on Apr 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ianlancetaylor@PumpkinSeed@gopherbot

        Issue actions

          encoding/json: Proposal - Flexible omitempty · Issue #45787 · golang/go