1. proto File
1// Specifies the version of the proto syntax, either 2 or 3
2syntax = "proto3";
3// Declares the import path for the generated Go code, which is the path used to import the pb.go file in other code
4option go_package = "internal/controller";
5// Package name
6package controller;
1@if "%DEBUG%"=="" @echo off
2@rem ##########################################################################
3@rem
4@rem Build proto
5@rem
6@rem ##########################################################################
7
8set DIRNAME=%~dp0
9cd /D %DIRNAME%
10cd ..
11
12set PROTOC_EXE=protoc.exe
13set REQUIRED_PROTOC_VERSION=3.20.3
14
15REM Set the paths for the proto and pb directories
16set PROTO_DIR=proto
17set PB_DIR=pb
18
19@REM Check if the protoc command exists
20%PROTOC_EXE% --version >NUL 2>&1
21if %ERRORLEVEL% neq 0 (
22 echo protoc is not installed or not in your PATH.
23)
24
25@REM Get the version of protoc
26for /f "tokens=2" %%i in ('%PROTOC_EXE% --version') do set PROTOC_VERSION=%%i
27
28@REM Check if the version matches
29if "%PROTOC_VERSION%"=="%REQUIRED_PROTOC_VERSION%" (
30 echo Found correct version of protoc: %PROTOC_VERSION%
31 goto execute
32) else (
33 echo Incorrect version of protoc. Required: %REQUIRED_PROTOC_VERSION%, but found: %PROTOC_VERSION%
34 exit /b 1
35)
36
37:execute
38@REM Delete all contents in the pb directory
39if exist %PB_DIR% (
40 rd /s /q "%PB_DIR%"
41)
42mkdir %PB_DIR%
43
44@REM Compile all proto files in the proto directory and place the generated pb files in the pb directory
45for /r %PROTO_DIR% %%f in (*.proto) do (
46 @REM The %%f here is the full absolute path, which needs to be split into a relative path
47 set "filepath=%%f"
48 setlocal enabledelayedexpansion
49 set "relpath=!filepath:*\proto=!"
50 set "relpath=!relpath:\=/!"
51 %PROTOC_EXE% ^
52 -I%PROTO_DIR% ^
53 --go_out=%PB_DIR% --go_opt=paths=source_relative^
54 --go-grpc_out=%PB_DIR% --go-grpc_opt=paths=source_relative ^
55 ./proto!relpath!
56 endlocal
57)
-I%PROTO_DIR%
indicates reading proto files from the PROTO_DIR directory.--go_out=%PB_DIR%
specifies the path where the generated Go code will be saved.--go_opt=paths=source_relative
means the output files will be placed in the same relative directory as the input files.
2. Compilation Errors
1google/api/annotations.proto:20:1: Import "google/protobuf/descriptor.proto" was not found or had errors.
2google/api/annotations.proto:28:8: "google.protobuf.MethodOptions" is not defined.
3google/protobuf/empty.proto: File not found.
After correctly installing protoc
and protoc-gen-go
, the error persists. The initial judgment is that there is an issue with the script.
1@REM Iterate through the directories in the proto directory for compilation
2for /D %%G in ("proto\*") do (
3 %PROTOC_EXE% ^
4 -I./proto ^
5 -I%GOPATH%/pkg/mod ^
6 -I%GOPATH%/pkg/mod/git.agilebot.com.cn/app/%APKG_NAME%/proto ^
7 --go_out=./proto --go_opt=paths=source_relative ^
8 --go-grpc_out=:./proto ^
9 --go-grpc_opt=paths=source_relative ^
10 --grpc-gateway_out=./proto ^
11 --grpc-gateway_opt=paths=source_relative ^
12 --openapiv2_out ./proto ^
13 --openapiv2_opt json_names_for_fields=false ^
14 --openapiv2_opt logtostderr=true ^
15 --openapiv2_opt output_format=yaml ^
16 ./proto/%%~nxG/*.proto
17)
The associated directory needs to be included using the -I
parameter. After copying the google
folder from the downloaded protoc/include
to the src
(newly created) folder under GOPATH
, add -I%GOPATH%/src^
. Running it again works fine.
3. Parameters
When calling protoc
with the go_out
flag, the protocol buffer compiler generates Go code. The protocol buffer compiler outputs the generated Go code to the directory specified by the go_out
command-line argument. The argument to the go_out
flag is the directory where you want the compiler to write the Go output. The compiler creates one source file for each .proto
file input. The name of the output file is created by replacing the .proto
extension with .pb.go
.
1protoc --proto_path=src --go_out=out --go_opt=paths=source_relative xxx.proto
–proto_path: Path to the proto source files
–go_out: Output directory
–go_opt: Additional options for the Go output