Getting Started
cwgo is a CloudWeGo All in one code generation tool that integrates the advantages of each component to improve the developer experience.
Prepare Golang development environment
- If you have not set up a Golang development environment before, you can refer to Golang Installation.
- It is recommended to use the latest version of Golang(support version >= v1.18).
- Make sure to enable go mod support (when Golang >= 1.15, it is enabled by default).
After completing the environment preparation, the next step will help you get started with cwgo quickly.
Install
go install github.com/cloudwego/cwgo@latest
It’s easiest to install with the go command, or you can choose to build and install from source yourself. To see where cwgo is installed, use:
go list -f {{.Target}} github.com/cloudwego/cwgo
To use thrift or protobuf’s IDL to generate code, you need to install the corresponding compiler: thriftgo or protoc.
thriftgo install:
GO111MODULE=on go install github.com/cloudwego/thriftgo@latest
protoc install
# brew install
brew install protobuf
# Official image installation, take macos as an example
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protoc-3.19.4-osx-x86_64.zip
unzip protoc-3.19.4-osx-x86_64.zip
cp bin/protoc /usr/local/bin/protoc
# Make sure include/google is placed under /usr/local/include
cp -r include/google /usr/local/include/google
After the installation is successful, execute cwgo --version
, thriftgo --version
, protoc --version
and you should be able to see the output of the specific version number (the version number is different, take x.x.x as an example):
cwgo --version
vx.x.x
thriftgo --version
vx.x.x
protoc --version
libprotoc x.x.x
Precautions
The bottom layer of cwgo uses kitex, hz, gen tools, so the corresponding tool specifications also need to be followed, such as kitex precautions and Notes for hz.
Using
For specific usage of cwgo, please refer to Command Line Tool.
Let’s take thrift as an example:
-
First create a directory
mkdir -p $GOPATH/src/local/cwgo_test cd $GOPATH/src/local/cwgo_test
-
Create an idl directory
mkdir idl
-
Write the idl/hello.thrift file
# idl/hello.thrift namespace go hello.example struct HelloReq { 1: string Name (api.query="name"); // Add api annotations to facilitate parameter binding } struct HelloResp { 1: string RespBody; } service HelloService { HelloResp HelloMethod(1: HelloReq request) (api.get="/hello"); }
-
Generate project layout
cwgo server --server_name a.b.c --type HTTP --idl idl/hello.thrift -module {{your_module_name}}
-
Compile and run
go mod tidy && go mod verify sh build.sh && sh output/bootstrap.sh
-
Initiate the call
curl http://127.0.0.1:8080/ping pong
Congratulations! So far you have successfully written a cwgo HTTP server and completed a call!