Only generate Go and TS pb file in this repo.
- Go 1.18 or higher
- Install Protocol Buffer Compiler (protoc) in your PATH.
- Install mage bin in your PATH.
Notice:
Usually, different versions ofprotocdo not have a significant impact, as they are compatible with each other.
- View the Go install documentation to install Go.
- Add
go/binto the PATH environment variable.
view the Proto Buffer Compiler Installation docs.
- Download the latest release of the Protocol Buffers compiler zip file corresponding to your OS and Arch.
- Unzip the file under
$HOME/.localor directory of your choice. - Update the PATH environment variable to include the
protocexecutable.
Using Go Install with Go version >= 1.18:
go install github.com/magefile/mage@latestWith Go version < 1.18:
You can use bootstrap_install_mage.bat or bootstrap_install_mage.sh to install mage fast.
-
Execute
mage InstallDependto install Go dependencies. -
Execute
mage GenGoto generate Go code. -
You can also view the Go Usage Docs for more information.
- Execute
npm install ts-prototo the workdir. - Execute
mage GenTypeScriptto generate TypeScript code.
In our example, we have a simple hello/hello.proto file:
syntax = "proto3";
// define a request message
message HelloRequest {
string name = 1;
UserInfo user = 2;
}
// define a response message
message HelloResponse {
string message = 1;
}
// define a parameter message
message UserInfo {
string name = 1;
int32 age = 2;
}
// define a service
service HelloService {
// define a rpc method
rpc SayHello (HelloRequest) returns (HelloResponse);
}- Write your method request and response messages. Like
HelloRequestandHelloResponse. - Write your service method. Like
SayHello. - You can also define the parameter message, like
UserInfo. - add the module name to
protoModulesvariable inmagefile.go. In this example, you need append"hello"inprotoModules. We recommend using the module name as the directory name and file name. - Execute corresponding languge command to generate protobuf code. More to view Compiling Your Protocol Buffers.