因此我们需要一种方法能通过在编写proto文件的时候,在里面注入tag,然后导出成go的时候这个被注入的字段的tag可以自定义。 解决方法 protoc-go-inject-tag:https://github.com/favadi/protoc-go-inject-tag 安装:go gethttps://github.com/favadi/protoc-go-inject-tag
打开一个新的终端窗口,运行以下命令来检查 protoc-go-inject-tag 是否已正确安装: bash protoc-go-inject-tag --version 如果命令返回了版本号信息,说明安装成功;如果返回 "command not found" 或其他错误信息,请检查环境变量配置是否正确,或者重新执行安装命令。通过...
说白了,官方觉得这个特性太 Go 语言定制化了,不愿意支持。 有什么替代方案 虽然现在 gogo 这一个开源项目已经进入废弃阶段,但根据 issues 内推荐的情况。可以选择继续使用 gogo,或者使用 protoc-go-inject-tag[3] 这一个项目。 简单例子,Proto 定义: // file: test.proto syntax = "proto3"; package pb; ...
通过对xxx.proto通过注释的形式加入验证tag(使用方式文档下方有说明), 然后再使用inject_tool.sh xxx.proto编译, 这样生成的xxx.pb.go文件中的struct注入自定义的tag 通过验证器对struct中的tag进行验证 2. 注入工具使用 先下载本项目:go get -u gitee.com/xuesongtao/protoc-go-valid ...
说白了,官方觉得这个特性太 Go 语言定制化了,不愿意支持。 有什么替代方案 虽然现在 gogo 这一个开源项目已经进入废弃阶段,但根据 issues 内推荐的情况。可以选择继续使用 gogo,或者使用protoc-go-inject-tag这一个项目。 简单例子,Proto 定义: // file: test.proto ...
go install github.com/favadi/protoc-go-inject-tag@latestor download the binaries from the releases page. Usage $protoc-go-inject-tag -hUsage of protoc-go-inject-tag:-XXX_skip stringtags that should be skipped (applies 'tag:"-"') for unknown fields (deprecated since protoc-gen-go v1.4....
v1.1.0: Upgrade golang/protobuf to v1.4.2 (#36) * upgrade github.com/golang/protobuf dependency to v1.4.2 * add protoc-go-inject-tag to code_gen.sh to test inject tags * update vendor with new version of golang/protobuf for travis * update travis with newer version of go * up...
通过自定义结构体tag来覆盖默认结构体tag:json:"xxx,omitempty"1. 修改proto文件,增加gotags注释在需要修改结构体tag的字段上增加注释:// @gotags: json:"...
在生成protocol文件后,执行如下命令,去除omitempty标签即可。 Get-ChildItem -Path "C:\path\to\directory" -Recurse -Filter "*.pb.go" | ForEach-Object { $file = $_.FullName $content = Get-Content -Path $file -Encoding UTF8 $newContent = $content -replace ',omitempty', '' ...
它们分为两种一般方法:1.使用相同的数据类型 1.使用两种不同的结构类型并在它们之间进行Map ...