I'm having problems with omitempty and empty values. Please see this playground example. I have a value which I don't want to be ignored during marshal in case of value "". This explicitly means that I want to clear the value and therefore I want to have marshalled result:
{"cf_objectType":"Product","cf_isLocked":"No","cf_ErrorMessage":""}
Now I tried the pointer-to-string approach here, but for some reason I don't like this. Are there any alternatives known? For example, why don't we have a tag (just like omitempty) like omitnull or something?
EDIT
To clarify, see below
m := Metadata{
ObjectType: "Product",
Locked: "No",
ErrorMessage: "",
}
I want the result of the marshal function on this struct to be:
{
"cf_objectType":"Product",
"cf_isLocked":"No",
"cf_ErrorMessage":""
}
AND
m := Metadata{
ObjectType: "Product",
Locked: "No",
}
result shoulde be:
{
"cf_objectType":"Product",
"cf_isLocked":"No",
}
omitemptyforErrorMessageyou'll get:{"cf_objectType":"Product","cf_isLocked":"No","cf_errorMessage":""}Is it what you need?ErrorMessageis implicitly initialized to its zero value,"". So the two examples are feeding identical data intoMarshal, so they will always yield identical output. If you don't like using a*string(though you don't state why), then even if there were anomitnil, it wouldn't help - you can't have a nil string, only a nil pointer, so you would still need to use a pointer to a string.stringand*stringin my struct definition. And I also can't "fill" the value without having thevar emptyString = "", right?