Struct tags in Go always seemed like a very bad and hacky way to add metadata to fields. I really hope that's something they'll look into in Go 2.
type Person {
Name string `protobuf:"name",json:"given_name"`
}Have you seen Protobuf options [1]? They allow for a tag-like-behaviour, but are strongly typed. It's notably used by gogoproto [2] to allow specifying custom field types or names [3] when generating Go stubs.
[1] - https://developers.google.com/protocol-buffers/docs/proto#cu...
Use it like:
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
...
Type myField = 1 [(gogoproto.jsontag) = "my-field"]; type Person struct {
FirstName string `json:"first_name"`
}
And you get json like `{"name": "Alice"}`, you can decode it something like this: var doc struct {
FirstName string `json:"name"`
}
json.Unmarshal(data, &doc)
person := Person(doc)
I have not looked at the assembly so I can't say whether this incurs extra overhead or not.ETA: Also, you already are needing to add code to every encode/decode point for the `map[string]interface{}` handling. :P