emicklei/proto

GitHub: emicklei/proto

一个用于解析 Google Protocol Buffers「.proto」定义文件的 Go 语言库,支持 proto2/proto3 及 editions 规范。

Stars: 615 | Forks: 69

# proto [![Go](https://static.pigsec.cn/wp-content/uploads/repos/cas/31/31c96a8fe552af584b5983afc39ddf2e71ca6806185aad0eda564c77331e5b3e.svg)](https://github.com/emicklei/proto/actions/workflows/go.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/emicklei/proto)](https://goreportcard.com/report/github.com/emicklei/proto) [![GoDoc](https://pkg.go.dev/badge/github.com/emicklei/proto)](https://pkg.go.dev/github.com/emicklei/proto) [![codecov](https://codecov.io/gh/emicklei/proto/branch/master/graph/badge.svg)](https://codecov.io/gh/emicklei/proto) 用于解析 Google Protocol Buffers 的 Go 包 [.proto 文件版本 2 + 3,editions](https://developers.google.com/protocol-buffers/docs/reference/proto3-spec) ### 安装 ``` go get github.com/emicklei/proto ``` ### 用法 ``` package main import ( "fmt" "os" "github.com/emicklei/proto" ) func main() { reader, _ := os.Open("test.proto") defer reader.Close() parser := proto.NewParser(reader) definition, _ := parser.Parse() proto.Walk(definition, proto.WithService(handleService), proto.WithMessage(handleMessage)) } func handleService(s *proto.Service) { fmt.Println(s.Name) } func handleMessage(m *proto.Message) { lister := new(optionLister) for _, each := range m.Elements { each.Accept(lister) } fmt.Println(m.Name) } type optionLister struct { proto.NoopVisitor } func (l optionLister) VisitOption(o *proto.Option) { fmt.Println(o.Name) } ``` ### 验证 当前的解析器实现并未完全验证 `.proto` 定义。 在多数(但非全部)情况下,当读取到意外的字符或 token 时,解析器会报告语法错误。 请使用一些 lint 工具或 `protoc` 进行完整验证。 ### 贡献 请参阅 [proto-contrib](https://github.com/emicklei/proto-contrib),了解在此包基础上的其他贡献,例如 protofmt、proto2xsd 和 proto2gql。 [protobuf2map](https://github.com/emicklei/protobuf2map) 是一个小型包,用于通过其 `.proto` 定义检查已序列化的 protobuf 消息。 © 2017-2025, [ernestmicklei.com](http://ernestmicklei.com)。采用 MIT License。欢迎贡献。
标签:EVTX分析, Go, Protocol Buffers, Ruby工具, SOC Prime, 开发工具, 数据序列化, 日志审计, 语法解析器