MyApps' Protocol Buffers & Type Code Generator
make # generate all
make gen-go # only generate go code based on proto
make gen-ts-onlytypes # only generate typescript code based on proto
0. setup environments
- Goalng
- Python
- Node.js
1. install protobuf for using protoc
command
# 1. install protobuf compiler
brew install protobuf
# 2. check version
protoc --version
2. install pkg protoc-gen-go
for generating Golang code, then add to $PATH
# 1. install pkg
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
# 2. add path
export PATH="$PATH:$(go env GOPATH)/bin" >> ~/.zshrc
source ~/.zshrc
# 3. check pkg's position & version
which protoc-gen-go
protoc-gen-go --version
3. install pkg ts-proto
for generating Typescript code
npm install
1. install pkg inside project
cd <project>
yarn add git+https://github.com/BlaxBerry/myapp_protobuf.git
yarn add -D google-protobuf @types/google-protobuf
2. import type then use
import type { MessageNode, HTMLNode } from "myapp_protobuf/ts/scenario";
import type { ChatBotData } from "myapp_protobuf/ts/chat";
import type { NoteData } from "myapp_protobuf/ts/notes";
1. get pkg
go get github.com/BlaxBerry/myapp_protobuf/go/scenario@main
2. import pkg then use
import (
"fmt"
scenario "github.com/BlaxBerry/myapp_protobuf/go/scenario"
)
var MockMessageNode = &scenario.MessageNode{
Id: 11,
Title: "xxx",
Text: "yyy",
}
var MockHTMLNode = &scenario.HTMLNode{
Id: 22,
Title: "xxx",
Text: "yyy",
}
func main() {
fmt.Println(MockMessageNode.Id)
fmt.Println(MockMessageNode.Title)
fmt.Println(MockMessageNode.Text)
}