protoc 插件编写之一 (protoc 插件的原理)


本文摘自网络,作者,侵删。

protoc的golang自动代码生成, 源码文件【google.golang.org/protobuf/types/pluginpb/plugin.pb.go】中有如下的一段文档,很好的解释了protoc插件的工作,和使用过程。

// protoc (aka the Protocol Compiler) can be extended via plugins.  A plugin is
// just a program that reads a CodeGeneratorRequest from stdin and writes a
// CodeGeneratorResponse to stdout.
//
// Plugins written using C++ can use google/protobuf/compiler/plugin.h instead
// of dealing with the raw protocol defined here.
//
// A plugin executable needs only to be placed somewhere in the path.  The
// plugin should be named "protoc-gen-${NAME}", and will then be used when the
// flag "--${NAME}_out" is passed to protoc.

// Code generated by protoc-gen-go. DO NOT EDIT.
// source: google/protobuf/compiler/plugin.proto

这段话翻译过来大致如下:

protoc可以通过插件扩展。插件的工作原理是从标准输入中读取 CodeGeneratorRequest, 处理后生成CodeGeneratorResponse ,并写入标准输出中。

插件必须按照 protoc-gen-{NAME} 格式命名,在使用protoc中将, --{NAME}_out 当作参数传给protoc, 就会起作用。

实际调用过程中,可以把生成的插件文件放在环境变量PATH中,也可直接通过--plugin=[ plugin-name=plugin-path, ....] 传给protoc

protoc --plugin=protoc-gen-NAME=path/to/mybinary --NAME_out=OUT_DIR

这样, protoc就知道要使用 protoc-gen-NAME, 并输出文件到 OUT_DIR 中去了


本文来自:简书

感谢作者:癞痢头

查看原文:protoc 插件编写之一 (protoc 插件的原理)

相关阅读 >>

Go语言入门:第一个 Go 程序

Golang中协程和线程的区别是什么?

手撸Golang 基本数据结构与算法 归并排序

Go string 解析

解决Golang中vendor引起的相同类型,却提示类型不一样问题

Golang nil 小知识

【paspberry pi】部署kubernetes

如何实现一个更全面的Golang版本的布谷鸟过滤器

手撸Golang Go与微服务 saga模式之4

Golang实现京东支付v2版本

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...