x
Scoop安装
1 | Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser |
scoop将软件包隔离在~\scoop目录中
对于终端应用程序,scoop在~\scoop\shims创建垫片,文件夹自动加入path环境变量,
对于图形应用程序,有scoop apps文件夹
buf安装
buf是protobuf的工具链与生态系统,让protobuf简单安全开发
1 | scoop install buf |
入门cli
安装示例
1 | git clone git@github.com:bufbuild/buf-examples.git |
git@github.com 使用ssh协议连接github,冒号后是组织名,仓库归属人,斜杠后是仓库名
start是进行修改的地方,finish用于存放最终状态,用于对比
配置workspace
1 | buf config init |
然后出现yaml文件
更改为
1 | version: v2 |
将模块指向proto子目录
1 | buf build |
generate
先创建buf.gen.yaml文件
1 | version: v2 |
然后生成
1 | buf generate |
lint
根据yaml文件规则检测.proto文件
1 | buf lint |
忽略依赖失败
如果目录里面没有报错文件,说明不是我拥有的文件,但是有应用(?)
就让buf lint跳过该文件
1 | version: v2 |
检测破坏性变更
1 | buf breaking --against ... |
实现api
初始化go.mod
1 | go mod init github.com/bufbuild/buf-examples |
实现server
1 | mkdir server |
1 | type nul > server\main.go |
win cmd换行用^,外层必须用双引号,内部双引号一个变两个
1 | buf curl --schema . --data "{""pet_type"": ""PET_TYPE_SNAKE"", ""name"": ""Ekans""}" http://localhost:8080/pet.v1.PetStoreService/PutPet |
入门bsr
添加Protovalidate依赖
1 | version: v2 |
deps和go.mod差不多
要在有yaml文件的目录里update
然后在.proto文件中导入Protovalidate
1 | import "buf/validate/validate.proto"; |
repeated 列表或数组,这个规则表示至少一条
生成代码,测试修复
1 | version: v2 |
1 | buf generate |
生成gen目录,然后启动服务器
1 | go run cmd/main.go |
1 | buf curl --data "{ \"invoice\": { \"customer_id\": \"bob\", \"line_items\": [{\"unit_price\": \"999\", \"quantity\": \"2\"}] }, \"tags\": { \"tag\": [\"spring-promo\",\"valued-customer\"] } }" --schema . --http2-prior-knowledge "http://localhost:8080/invoice.v1.InvoiceService/CreateInvoice" |
回显
1 | 2026/06/02 12:20:06 Creating invoice for customer bob for 1998 |
发布共享module
创建组织
1 | buf registry organization create buf.build/julien123-quickstart |
创建common仓库
1 | buf registry module create buf.build/julien123-quickstart/common --visibility public |
在server旁边创建同级目录
1 | cd .. |
Protocol Buffers
和xml类似
如有错误,多多指教