When using protoc-gen-go to generate the go code from the proto file, the json's key name will remain as the key specified in the proto file. In the official field name snake case is recommended. https://developers.google.com/protocol-buffers/docs/style
However, I would like json's key name to be snake case.
When I check the generator code of protoc-gen-go, I certainly set the field name as it is.
jsonName := *field.Name
tag := fmt.Sprintf("protobuf:%s json:%q", g.goTag(message, field, wiretype), jsonName+",omitempty")
This would be the ideal solution for me:
jsonName := field.GetJsonName()
How can I get the field name in snake case?
This problem was solved.
By using gogo/protobuf(https://github.com/gogo/protobuf).
I used gogoproto.jsontag
.
You are calculating the tag name yourself, so you can convert it yourself. There is already a package on github that does the job: https://github.com/iancoleman/strcase
To convert the name yourself:
import "github.com/iancoleman/strcase"
jsonName := strcase.ToSnake(*field.Name)