Suppose I have a struct Foo
.
Foo struct {
Bar, Baz int
}
And I want to marshal this struct into json
like so:
{bar : 1, baz : 2}
How could I achieve that, without splitting my single line multiple name declaration (Bar, Baz int
) into 2 separate lines using tags.
This works:
Foo struct { Bar int `json:"bar"` Baz int `json:"baz"` }
But I'd like:
Foo struct { Bar, Baz int `json:???` }
Is the latter even possible?
According to the specification, No.
StructType = "struct" "{" { FieldDecl ";" } "}" .
FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] .
AnonymousField = [ "*" ] TypeName .
Tag = string_lit .
go has a strict syntax favouring the one way to do things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With