How to Turn a .proto File into Go Structs
Sometimes you need the shape of a Protocol Buffers message in Go without pulling in the protobuf runtime: a REST handler that receives the JSON form, a test fixture, or a quick script. This page reads the .proto file and writes plain structs for it, with names and json tags that line up with what protobuf itself would use.
Paste or upload the .proto file
Paste a schema into the left editor, upload a .proto file (up to 10 MB), or press Sample. proto2, proto3 and edition files all work, and a snippet without a syntax line is read as proto3.
Example: a small order schema
syntax = "proto3";
package shop.v1;
option go_package = "example.com/shop/shopv1";
message Order {
int64 id = 1;
string customer_id = 2;
repeated Item items = 3;
optional string coupon = 4;
map<string, string> labels = 5;
}
message Item {
string sku = 1;
uint32 qty = 2;
double price = 3;
}
Check the Go structs
The right panel shows one struct per message and a named type with constants per enum, with json tags that use protobuf's JSON field names. Anything that needs your attention, such as a service that was left out or a type from a file you didn't paste, is listed above the code. If the file has a mistake you get the line and column instead.
What the page writes for that schema
customer_id becomes CustomerID with the JSON name customerId, the optional coupon is a pointer, and the package name comes from go_package.
// Plain Go structs for the messages in this .proto file, with json tags that follow
// protobuf's JSON field names. They are for encoding/json, not the binary wire format:
// for proto.Marshal and gRPC, generate code with protoc and protoc-gen-go.
//
// Note: 64-bit integers are int64/uint64, so long IDs such as 8901240544102066246 keep every digit. protobuf's own JSON writer (protojson) puts 64-bit numbers in quotes; to read that JSON, add ,string to the json tag of each single (non-repeated) 64-bit field.
package shopv1
// Order is the message shop.v1.Order.
type Order struct {
ID int64 `json:"id,omitempty"`
CustomerID string `json:"customerId,omitempty"`
Items []*Item `json:"items,omitempty"`
Coupon *string `json:"coupon,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}
// Item is the message shop.v1.Item.
type Item struct {
Sku string `json:"sku,omitempty"`
Qty uint32 `json:"qty,omitempty"`
Price float64 `json:"price,omitempty"`
}
Copy or download the code
Copy the code or download protobuf_types.go and drop it into your package. It only uses the Go standard library.
var order shopv1.Order
if err := json.Unmarshal(body, &order); err != nil {
return err
}
fmt.Println(order.ID, order.CustomerID, len(order.Items))Need the full protobuf runtime instead? Run protoc with the protoc-gen-go plugin. To check a schema before converting it, try the Proto File Validator, and if you only have JSON data, JSON to Go builds structs straight from a sample document.
Frequently Asked Questions
Is this the same code protoc-gen-go generates?
No. protoc-gen-go writes types that implement proto.Message and carry internal state, which you need for the binary wire format and for gRPC. This page writes plain structs that encoding/json can read and write, which is handy for REST handlers, test fixtures or tools that only see the JSON form of a message. Type names follow protoc-gen-go (Order_LineItem for a nested message, Status_STATUS_PAID for an enum value), while field names use Go initialisms, so user_id becomes UserID rather than UserId.
How are .proto types turned into Go types?
int32, sint32 and sfixed32 become int32; int64, sint64 and sfixed64 become int64; uint32 and fixed32 become uint32; uint64 and fixed64 become uint64. float is float32, double is float64, bool, string and bytes map straight across ([]byte is Base64 in JSON). repeated fields are slices, map<K, V> fields are Go maps, other messages are pointers, and enums are named int32 types with one constant per value. google.protobuf.Timestamp becomes *time.Time, Struct becomes map[string]any and the wrapper types become pointers such as *string.
Do long 64-bit IDs keep all their digits?
Yes. A field declared int64 or uint64 is int64 or uint64 in Go, so a value like 8901240544102066246 or 18446744073709551615 is read by encoding/json exactly. One thing to know: protobuf's own JSON writer (protojson) puts 64-bit numbers in quotes. To read that output, add ,string to the json tag of those fields, as the note in the generated code explains.
How do optional fields and oneofs come out?
Fields marked optional in proto3, and all optional fields in proto2, become pointers, so you can tell an unset field from one that is set to zero or an empty string. The members of a oneof are written as pointer fields under a comment that names the oneof; only one of them should be set. Enum types read and write their value names in JSON, the way protobuf does, and also accept plain numbers.
What happens to services, imports and options?
Services and their RPCs are left out with a note, because gRPC client and server code comes from protoc-gen-go-grpc. A message type from an imported file that you did not paste becomes json.RawMessage, so the code still compiles; paste the imported file as well to get real structs. The Go package name comes from option go_package when there is one, otherwise from the proto package. Default values and extensions are not carried over.
Is my .proto file uploaded anywhere?
No. The conversion runs in your browser and the file is not sent to our server. Nothing is kept either: when you reload the page, the editor starts empty.