cwgo is a CloudWeGo All in one code generation tool that integrates the advantages of each component to improve the developer experience.
After completing the environment preparation, the next step will help you get started with cwgo quickly.
$ 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
First, we need to install the command-line code generation tools needed to use this example:
GOPATH
environment variable has been defined correctly (e.g. export GOPATH=~/go
) and add $GOPATH/bin
to the PATH
environment variable (e.g. export PATH=$GOPATH/ bin:$PATH
); do not set GOPATH
to a directory that the current user does not have read and write permissionsgo install github.com/cloudwego/cwgo@latest
go install github.com/cloudwego/thriftgo@latest
After the installation is successful, execute cwgo --version
and thriftgo --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
$GOPATH/src
, you need to create an additional directory under $GOPATH/src
, and then get the code after entering this directory:$ mkdir -p $(go env GOPATH)/src/github.com/cloudwego
$ cd $(go env GOPATH)/src/github.com/cloudwego
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.
For specific usage of cwgo, please refer to Command Line Tool
Let’s take thrift as an example
$ mkdir -p $GOPATH/src/local/cwgo_test
$ cd $GOPATH/src/local/cwgo_test
$ mkdir idl
# 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");
}
static command line
$ cwgo server -service=a.b.c -type HTTP -idl=idl/hello.thrift
dynamic command line
$ go mod tidy && go mod verify
$ sh build.sh && sh output/bootstrap.sh
$ curl http://127.0.0.1:8080/ping
pong
Congratulations! So far you have successfully written a Cwgo server and completed a call!