1

foreword

This article is based on An Qiansong's video sharing. The video playback address is as follows:

https://www.bilibili.com/video/BV1Hr4y1x7Ne/?aid=769539108&cid=732286357&page=1

Origin of goctl

1. The birth of goctl

The earliest function of goctl is to solve the problem of GRPC internal network debugging. It was around 2019. In our production environment, RPC is isolated from the internal network and cannot be accessed through the external network. In order to quickly mock some online RPC client requests , it simply implements the code generation of the first version, the main purpose is to access the RPC Server to do some debugging.

2. Why do you need goctl?

  • Reduce communication costs

    Communication is a form of team collaboration for information exchange. There are many ways to communicate, such as meeting communication, document communication, and chat communication. I believe that no matter which method is used, communication is the most difficult part of the team. Meeting communication requires It takes up a lot of time, and it takes half an hour to start. Document communication also takes up a lot of time to conceive and write a large-scale document. In the end, the expected goal may not be expressed. Online chat requires both parties to be online to exchange information. , of course, the information we communicate and exchange here refers to some content in development, such as interface information, deployment information, etc.

  • Reduce team coupling

    With communication, the coupling of collaboration between teams is unavoidable. For example, in front-end and back-end development, the biggest coupling is interface coupling. After the front-end completes the specified UI drawing, it needs to wait for the back-end interface to be deployed to the corresponding environment. In order to realize the debugging of functions, during this period, the resources of the front-end team will be greatly wasted, which will also lead to problems such as project delays.

  • Improve development efficiency

    In addition to communication costs and team coupling, each team also spends a lot of time doing repetitive work during project development. For example, when we develop a new function, we need to define interfaces, write interface documents, and prepare for coding. Business development, model files, writing Dockerfile files, and writing k8s yaml files, we can have room for improvement in each link, allowing users to focus their real time on business development.

  • reduce error rates

    In the previous development practice, the problem of incomplete implementation of grpc server often occurred, and the implementation class of grpc server often failed to compile; in addition, in the development of database query layer code, the writing of SQL statements required more parameters and less parameters. , the parameters are misplaced, which is difficult to find during the compilation process. Generally, it may not be found until the QA link, and even worse, it will lead to online problems.

3. How to understand the development specification?

Development specifications, including coding specifications, engineering specifications, submission specifications, code review specifications, deployment specifications, etc. If the development specifications are unified within the team, the benefits can be imagined. For example: if your company does not have unified development specifications Specifications, you need to do some cross-departmental collaboration or support at this time. The new development environment makes you discouraged, and you have the idea of resistance even before it starts. This is not against the original intention of support.

4. How to understand engineering efficiency?

Engineering efficiency, to understand engineering efficiency, you can first look at the definition of "efficiency":

Efficiency is the amount of work done per unit of time

The goal of efficiency is to be fast, but fast is not efficiency. In other words, it is the high-quality work completed in unit time. This is the goal we want to pursue. Then engineering efficiency is to "shorten the distance from development to online".

Second, the installation and function introduction of goctl

1 Introduction

  • goctl definition

    The definition of goctl, to be precise, positioning, can be understood as a code generation scaffold. Its core idea is to complete the code generation function through syntax analysis, semantic analysis and data-driven implementation of DSL, aiming to help developers improve development from the perspective of engineering efficiency. developer's development efficiency.
  • solved problem

    • Improve development efficiency: The code generation of goctl covers Go, Java, Android, iOS, TS and other languages. Through goctl, the code of each terminal can be generated with one click, allowing developers to focus on business development.
    • Reduce communication costs: Use zero-api to replace API documents, and each end generates code for each end with zero-api as the center, eliminating the need for interface information transmission through conferences and API documents.
    • Reduce coupling: In the previous development practice, before goctl, we used traditional API documents as the carrier of interface information transmission, and then conducted a second review through meetings. We all know that in front-end development work, in addition to Outside of UI drawing is UI joint debugging. If you are a more active front-end developer, you may use some scaffolding or mock some data to conduct UI joint debugging. On the contrary, there are also cases where you wait for the back-end deployment before joint debugging. The reason is that writing the mock data yourself is too time-consuming and I don’t want to move it. If the development progress of the front-end is ahead of the back-end, then during the deployment of the back-end to the environment, the front-end can only wait quietly in place. This is not only a kind of The waste of resources will also cause the delay of the project progress. With zero-api, the front end can use goctl to generate an appropriate amount of mock data for UI joint debugging.
  • Development History

The development history of goctl can be briefly reviewed, let’s pick a few iconic functions and talk about it!

The first is goctl rpc, the first version of goctl rpc is the prototype of goctl, mainly to generate client code to debug rpc, and the details will not be expanded. The development of goctl rpc has gone through many twists and turns. The most impressive one should be the transition from goctl rpc proto to goctl rpc protoc. During this period, in order to solve many problems, corresponding changes were made. The specific details are shared in the rpc code generation and use. To share in detail, in fact, the most impressive feature for me is the goctl model. It is a starting point for me to start maintaining goctl, and it is also a solution I discovered from business development practice. After all, I still I was "lazy". I remember developing a business module at that time. Because the business was relatively large, there were a lot of data tables to be created. Before there was no goctl, I had to manually code it myself. I still remember this part, it took only one day. It takes time to write the model file, because I don’t have the habit of using orm, so this piece of sql is completely a bare sql statement. After the service runs, I find that there are many errors in the sql parameter transmission, which are basically the problem of more parameters and fewer parameters. In addition, every time an index is added or a field is changed, the related logic maintenance of the cache also gives me a lot of headaches, so combined with the development best practices at that time, I hand over this piece of work to goctl according to whether it is cached or not. The version goctl model is mainly generated by the web version, and it is integrated into goctl one after another, which has brought great development efficiency benefits to my subsequent development.

  • Looking to the future

    goctl integrates a lot of functions, it is basically based on code generation, supplemented by development specifications, goctl is based on zero-api as the center for code generation, so in the future, the construction of zero-api will become our focus:

    • The transformation of ast: replace the original heavy antlr4, which can solve various problems that many people do not have enough memory when installing goctl and the system architecture does not support it.
    • The support of goctl mock, decoupling the dependency coupling of each end to the maximum extent

      2. Installation

      There are two ways to install Goctl, go get or go install, followed by docker.

go get/install

 # Go 1.16 之前版本
$ GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/zeromicro/go-zero/tools/goctl@latest

# Go 1.16 及以后版本
$ GOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/go-zero/tools/goctl@latest

# macOS
$ brew install goctl

# 验证
$ goctl --version
goctl version 1.3.5 darwin/amd64

# 查看 goctl
$ cd $GOPATH/bin && ls | grep "goctl"
If executing goctl --version prompts an error of command not found: goctl, please check whether $GOBIN is in the environment variable first.

docker

 # 1. pull
$ docker pull kevinwan/goctl
....

# 2. ls
$ docker image ls | grep "goctl"
kevinwan/goctl        latest    7fd46843de3d   5 weeks ago    383MB

# 3. run
$ docker run -it kevinwan/goctl /bin/sh

3. Function introduction

Let's take a look at the function introduction. We can list the functions that we currently support through the command goctl --help. Maybe you didn't pay attention to this command when you read the document, but only used some high-frequency commands. Today Since it is a topic sharing of goctl, we will spend some time together and take everyone to get familiar with goctl from 0 to 1.

 $ goctl --help                                                       
A cli tool to generate api, zrpc, model code

Usage:
goctl [command]

Available Commands:
api         Generate api related files
bug         Report a bug
completion  Generate the autocompletion script for the specified shell
docker      Generate Dockerfile
env         Check or edit goctl environment
help        Help about any command
kube        Generate kubernetes files
migrate     Migrate from tal-tech to zeromicro
model       Generate model code
quickstart  quickly start a project
rpc         Generate rpc code
template    Template operation
upgrade     Upgrade goctl to latest version

Flags:
-h, --help      help for goctl
-v, --version   version for goctl

Use "goctl [command] --help" for more information about a command.
goctl has rich functions, so it is inevitable that there are many command parameters. In addition to reading the documentation, you can also make good use of the --help flag, because it will tell you the usage of each command or sub-command, what parameters are required, and the parameters Whether the type is a string or a boolean, whether it is required or optional, you must not guess what flag he should use. Although goctl is my main maintenance, in fact, many times, some commands are compared with documents, and there are not many features to update. I also I can't remember, I also used --help to view the function introduction

Key demo

goctl completion

Check goctl version

 $ goctl --version
goctl version 1.3.5 darwin/amd64
1.3.5
The content in this section is only suitable for the reference of auto-completion settings for goctl version 1.3.5 and earlier. Version 1.3.5 and earlier only supports Unix-like operating systems, and does not support Windows auto-completion. After 1.3.5, please skip Contents of this section
  1. Execute goctl completion -h to view the usage instructions
 $ goctl completion --help                                
   NAME:
   goctl completion - generation completion script, it only works for unix-like OS

USAGE:
goctl completion [command options] [arguments...]

OPTIONS:
--name value, -n value  the filename of auto complete script, default is [goctl_autocomplete]

According to the help description, goctl completion supports passing a parameter of --name to generate the file name of the auto-completion script, but this parameter is not required. If it is not filled in by default, it is the goctl_autocomplete file, which is located in the $GOCTLHOME directory.

  1. Execute goctl completion to generate autocompletion scripts
 $ goctl completion
generation auto completion success!
executes the following script to setting shell:
echo PROG=goctl source /Users/keson/.goctl/.auto_complete/zsh/goctl_autocomplete >> ~/.zshrc && source ~/.zshrc

# 或者
echo PROG=goctl source /Users/keson/.goctl/.auto_complete/bash/goctl_autocomplete >> ~/.bashrc && source ~/.bashrc
  1. View current shell
 $ echo $SHELL                                                  
/bin/zsh
  1. Select the script execution corresponding to the shell, I am zsh here, so the execution instructions are as follows
 $ echo PROG=goctl source /Users/keson/.goctl/.auto_complete/zsh/goctl_autocomplete >> ~/.zshrc && source ~/.zshrc
  1. Restart the terminal to verify the result
 # 键入 goctl 后按下 tab 键查看自动补全提示
$ goctl # tab
api            -- generate api related files
bug            -- report a bug
completion     -- generation completion script, it only works for unix-like OS
docker         -- generate Dockerfile
env            -- check or edit goctl environment
help        h  -- Shows a list of commands or help for one command
kube           -- generate kubernetes files
migrate        -- migrate from tal-tech to zeromicro
model          -- generate model code
rpc            -- generate rpc code
template       -- template operation
upgrade        -- upgrade goctl to latest version
1.3.6
The content of this section is only suitable for the reference of auto-completion settings of goctl version 1.3.6 and later. Before 1.3.6, please skip this section.
  1. If you are upgrading from 1.3.5 to 1.3.6 and later, you need to execute the following command to clear the auto-completion configuration of the old version. Here we take zsh as an example, otherwise please skip this step
 # 编辑 ~/.zshrc
$ vim ~/.zshrc
# 找到 PROG=goctl 所在行并删除
# source
$ source ~/.zshrc && rm .zcompdump*
# 重启终端
  1. Execute goctl completion -h to view the usage instructions. From the usage help, you can view the auto-completion that currently supports bash, fish, powershell, and zsh shells.
 $ goctl completion --help
Generate the autocompletion script for goctl for the specified shell.
See each sub-command's help for details on how to use the generated script.

Usage:
goctl completion [command]

Available Commands:
bash        Generate the autocompletion script for bash
fish        Generate the autocompletion script for fish
powershell  Generate the autocompletion script for powershell
zsh         Generate the autocompletion script for zsh

Flags:
-h, --help   help for completion

Use "goctl completion [command] --help" for more information about a command.
  1. View currently used shell
 $ echo $SHELL                                                  
/bin/zsh
  1. Since zsh is currently in use, let's take a look at zsh's autocompletion settings help
 $ goctl completion zsh --help
Generate the autocompletion script for the zsh shell.

If shell completion is not already enabled in your environment you will need
to enable it.  You can execute the following once:

        echo "autoload -U compinit; compinit" >> ~/.zshrc

To load completions for every new session, execute once:

#### Linux:

        goctl completion zsh > "${fpath[1]}/_goctl"

#### macOS:

        goctl completion zsh > /usr/local/share/zsh/site-functions/_goctl

You will need to start a new shell for this setup to take effect.

Usage:
goctl completion zsh [flags]

Flags:
-h, --help              help for zsh
--no-descriptions   disable completion descriptions

As can be seen from the above, depending on the operating system, the setting method of auto-completion is also different. I am macOS here. Let's execute the corresponding command:

 $ goctl completion zsh > /usr/local/share/zsh/site-functions/_goctl

Let's reopen a terminal to try:

 # 键入 goctl 后按下 tab 键查看自动补全提示
$ goctl # tab
api         -- Generate api related files
bug         -- Report a bug
completion  -- Generate the autocompletion script for the specified shell
docker      -- Generate Dockerfile
env         -- Check or edit goctl environment
help        -- Help about any command
kube        -- Generate kubernetes files
migrate     -- Migrate from tal-tech to zeromicro
model       -- Generate model code
quickstart  -- quickly start a project
rpc         -- Generate rpc code
template    -- Template operation
upgrade     -- Upgrade goctl to latest version

Common mistakes

  1. goctl Error: unknown flag: --generate-goctl-completion
  2. (eval):1: command not found: _goctl
    Reconfigure according to 1.3.6 Auto-completion configuration

goctl migrate

Help developers to seamlessly migrate from versions before 1.3.0 to any version after 1.3.0. If the go-zero version used is 1.3.0 and later, there is no need to do this.

Why do you need to migrate?

go-zero has made organizational changes in version 1.3.0, that is, replacing the original tal-tech organization name with zeromicro.

I now have a demo project awesomemigrate generated with an old version of goctl, the go-zero module of this project is
 module awesomemigrate

go 1.18

require github.com/tal-tech/go-zero v1.2.5

...

Suppose we need to upgrade the project's go-zero to the latest version 1.3.3 so far. Before completing the upgrade of the project from tal-tech to zeromicro, we need to ensure that the goctl version is greater than v1.3.2, and then execute the goctl migration instruction. If If the goctl version is less than v1.3.2, it needs to be upgraded.

 # 1. 查看当前 goctl 版本
$ goctl --version
goctl version 1.2.5 darwin/amd64

# 2. 由于 goctl 版本小于 v1.3.2,则需要升级至最新,如果 goctl 版本已经是1.3.2及之后了,则可以不用升级
$ go install github.com/zeromicro/go-zero/tools/goctl@latest

# 3. 查看一下迁移指令使用帮助
$ goctl migrate --help                                  
NAME:
goctl migrate - migrate from tal-tech to zeromicro

USAGE:
goctl migrate [command options] [arguments...]

DESCRIPTION:
migrate is a transition command to help users migrate their projects from tal-tech to zeromicro version

OPTIONS:
--verbose, -v    verbose enables extra logging
--version value  the target release version of github.com/zeromicro/go-zero to migrate

# 4. 进入待迁移的项目下,执行迁移指令
$ goctl migrate --version 1.3.3

# 5. 验证依赖是否存在问题
$ go test .
?       awesomemigrate  [no test files]

After the migration is complete, let's look at the module file again

 module awesomemigrate

go 1.18

require github.com/zeromicro/go-zero v1.3.3
...

Searching for the matching result of tal-tech in the project will find 0:

goctl env

goctl env is mainly used for environment detection, installation, environment parameter control and other functions. In addition, it can also view some environmental information of current goctl, so that users can report the current environment of goctl according to this environment when encountering bugs.

First, let's take a look at its instructions for use

 $ goctl env --help                                    
NAME:
goctl env - check or edit goctl environment

USAGE:
goctl env command [command options] [arguments...]

COMMANDS:
install  goctl env installation
check    detect goctl env and dependency tools

OPTIONS:
--write value, -w value  edit goctl environment
--help, -h               show help

goctl env supports several functions of environment viewing, parameter modification, dependency detection and installation. Let's take a look at them in turn

1. Environment view

In the case of not entering any flag, it is to view the current goctl environment.

 $ goctl env                                             
GOCTL_OS=darwin                        
GOCTL_ARCH=amd64
GOCTL_HOME=/Users/keson/.goctl
GOCTL_DEBUG=false
GOCTL_CACHE=/Users/keson/.goctl/cache
GOCTL_VERSION=1.3.5
PROTOC_VERSION=3.19.4
PROTOC_GEN_GO_VERSION=v1.27.1
PROTO_GEN_GO_GRPC_VERSION=1.2.0

The approximate description of the above parameters is

parameter name Parameter Type Parameter Description
GOCTL_OS STRING Current operating system, common values are darwin, windows, linux, etc.
GOCTL_ARCH STRING Current system architecture, common values are amd64, 386, etc.
GOCTL_HOME STRING Defaults to ~/.goctl
GOCTL_DEBUG BOOLEAN Whether to enable the debug mode, the default is false, it is not currently used, mainly because it may be used to control the debug mode in goctl development
GOCTL_CACHE STRING goctl cache directory, mainly cache downloaded dependencies protoc, protoc-gen-go, protoc-gen-go-grpc, etc.
GOCTL_VERSION STRING current goctl version
PROTOC_VERSION STRING The current protoc version, or an empty string if not installed
PROTOC_GEN_GO_VERSION STRING Current protoc-gen-go version, empty string if not installed or installed from github.com/golang/protobuf before version v1.3.2
PROTO_GEN_GO_GRPC_VERSION STRING Current protoc-gen-go-grpc version, or empty string if not installed
2. Modify parameters

For example, we change GOCTL_DEBUG to true

 # 修改前
$ goctl env | grep 'GOCTL_DEBUG'
GOCTL_DEBUG=false

# 修改 GOCTL_DEBUG 为 true
$ goctl env -w GOCTL_DEBUG=true

# 修改后
goctl env | grep 'GOCTL_DEBUG'
GOCTL_DEBUG=true
3. Dependency detection/installation

Let's check whether my current dependencies are installed. The current dependency detection content is protoc, protoc-gen-go, protoc-gen-go-grpc

 # 我们来检查一下依赖安装
$ goctl env check --verbose                              
[goctl-env]: preparing to check env

[goctl-env]: looking up "protoc"
[goctl-env]: "protoc" is not found in PATH

[goctl-env]: looking up "protoc-gen-go"
[goctl-env]: "protoc-gen-go" is not found in PATH

[goctl-env]: looking up "protoc-gen-go-grpc"
[goctl-env]: "protoc-gen-go-grpc" is not found in PATH

[goctl-env]: check env finish, some dependencies is not found in PATH, you can execute
command 'goctl env check --install' to install it, for details, please execute command
'goctl env check --help'

# 安装依赖,安装依赖有2中方式
# 方式一
# $ goctl env check --install --force --verbose

# 方式二
$ goctl env install --verbose -f                       
[goctl-env]: preparing to check env

[goctl-env]: looking up "protoc"
[goctl-env]: "protoc" is not found in PATH
[goctl-env]: preparing to install "protoc"
[goctl-env]: "protoc" is already installed in "/Users/keson/go/bin/protoc"

[goctl-env]: looking up "protoc-gen-go"
[goctl-env]: "protoc-gen-go" is not found in PATH
[goctl-env]: preparing to install "protoc-gen-go"
[goctl-env]: "protoc-gen-go" is already installed in "/Users/keson/go/bin/protoc-gen-go"

[goctl-env]: looking up "protoc-gen-go-grpc"
[goctl-env]: "protoc-gen-go-grpc" is not found in PATH
[goctl-env]: preparing to install "protoc-gen-go-grpc"
[goctl-env]: "protoc-gen-go-grpc" is already installed in "/Users/keson/go/bin/protoc-gen-go-grpc"

[goctl-env]: congratulations! your goctl environment is ready!

goctl-rpc

First, let's take a look at the help of this command

 goctl rpc -h                                                       
Generate rpc code

Usage:
goctl rpc [flags]
goctl rpc [command]

Available Commands:
new         Generate rpc demo service
protoc      Generate grpc code
template    Generate proto template

Flags:
--branch string   The branch of the remote repo, it does work with --remote
-h, --help            help for rpc
--home string     The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
--o string        Output a sample proto file
--remote string   The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure

Use "goctl rpc [command] --help" for more information about a command.

This instruction provides three sub-instructions, new is to quickly create a zrpc service, protoc is to generate zrpc code according to the proto description file, and template is to quickly generate a proto template, we will focus on the goctl rpc protoc instruction to expand, take a look at goctl Instructions for using rpc protoc:

 goctl rpc protoc -h                                              
Generate grpc code

Usage:
goctl rpc protoc [flags]

Examples:
goctl rpc protoc xx.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=.

Flags:
--branch string     The branch of the remote repo, it does work with --remote
-h, --help              help for protoc
--home string       The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
--remote string     The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
--style string      The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md] (default "gozero")
-v, --verbose           Enable log output
--zrpc_out string   The zrpc output directory

How to use goctl rpc protoc? Why are there --go_out and --go-grpc_out parameters in the example, but I didn't see an introduction when I looked at help? At present, let's understand this first. Let's put goctl aside first. What are the instructions for generating grpc code according to the official protoc? Which flags will be used? For the code generation of zrpc, you can understand that goctl generates zrpc by adding the goctl rpc prefix and some flags required by goctl to generate zrpc on the basis of generating grpc code instructions. For protoc and the plugin protoc-gen The related parameters of -go and protoc-gen-grpc-go goctl are only for inheritance, and there is no need to describe them again in help. Later, when analyzing the source code, we can explain some of the design and considerations of this piece in detail. Next, we To demonstrate with an example, suppose we have a greet.proto file, aside from goctl, the instructions we need to execute to generate grpc code are as follows:

If you don't know how the grpc code is generated, it is recommended to refer to the official documentation https://grpc.io/
 # 进入 greet.proto 所在目录
$ protoc greet.proto --go_out . --go-grpc_out .
$ tree
.
├── greet.proto
└── pb
├── greet.pb.go
└── greet_grpc.pb.go

Then according to the introduction of goctl rpc protoc above, our instruction to generate zrpc code should be

 # 这里多了一个 --zrpc_out 用于指定 zrpc 代码的输出目录
$ goctl rpc protoc greet.proto --go_out=. --go-grpc_out=. --zrpc_out=. --verbose
[goctl-env]: preparing to check env

[goctl-env]: looking up "protoc"
[goctl-env]: "protoc" is installed

[goctl-env]: looking up "protoc-gen-go"
[goctl-env]: "protoc-gen-go" is installed

[goctl-env]: looking up "protoc-gen-go-grpc"
[goctl-env]: "protoc-gen-go-grpc" is installed

[goctl-env]: congratulations! your goctl environment is ready!
[command]: protoc greet.proto --go_out . --go-grpc_out .
Done.
$ tree
.
├── etc
│   └── greet.yaml
├── go.mod
├── greet
│   └── greet.go
├── greet.go
├── greet.proto
├── internal
│   ├── config
│   │   └── config.go
│   ├── logic
│   │   └── pinglogic.go
│   ├── server
│   │   └── greetserver.go
│   └── svc
│       └── servicecontext.go
└── pb
├── greet.pb.go
└── greet_grpc.pb.go

8 directories, 11 files
If you need to output the log when generating code, you can add --verbose to display the log.
goctl rpc protoc will first detect your environment dependencies when generating zrpc code. If it is not installed, it will automatically install the dependencies and then generate the code.

4. Editor plugin

In order to improve the efficiency of writing zero-api files, we have provided corresponding editor plugins for intellij and vscode respectively. For the introduction and use of intellij plugins, please refer to https://github.com/zeromicro/goctl-intellij , vscode plugin introduction And please refer to https://github.com/zeromicro/goctl-vscode/blob/main/README-cn.md

3. Problems encountered in the use of goctl

Goctl has had 13 first-level instructions and nearly 30 second-level instructions from the original function rpc proxy to the current version (v1.3.5). During this period, goctl has made some adjustments, and gcotl itself is also moving very fast. He It is more like moving forward in exploration and developing towards a faster and better usable direction. Therefore, on the road of iteration, goctl will appear a little unstable, and you may encounter many problems. Here is a summary of your feedback in the community. More questions to share.

1. Failed to install goctl on 386 architecture!

Description: In the source code generated by antlr, the boundary value of uint of 386 architecture is not well handled, resulting in boundary overflow

Fixed version: v1.3.3

2. What version of the plugin is installed on grpc?

Description: Anyone familiar with grpc should know that the plugin protoc-gen-go that generates grpc code has two repositories under maintenance, three installation sources, and two maintenance repositories:

 # 1. golang
# github.com/golang/protobuf/protoc-gen-go
# 2. protocolbuffers
# github.com/protocolbuffers/protobuf-go/cmd/protoc-gen-go

The three installation sources are

 # 1. golang 维护的仓库,目前已不推荐使用
# github.com/golang/protobuf/protoc-gen-go
# 2. protocolbuffers 维护的,目前推荐使用的
# github.com/protocolbuffers/protobuf-go/cmd/protoc-gen-go
# 3. goolge 安装,这其实和第二种安装的是一个二进制,他的源码就是protocolbuffers 维护的相同仓库
# google.golang.org/protobuf/cmd/protoc-gen-go

Before v.1.3.4, if you use goctl rpc proto to generate zrpc code, it is recommended to install the old version of the plug-in, which is maintained by golang, so the command to generate zrpc code is goctl for the simplicity of user-generated instructions, and has made a thick package , but the accompanying problem is that it is difficult to be compatible, so this instruction is deprecated.

In v.1.3.4 and later, there is no restriction on protoc-gen-go, users can freely choose different sources to install, but it is recommended to install the version maintained by protocolbuffer (the official document has been replaced with this), and it must be installed protoc-gen-grpc-go plugin, the purpose of this is to follow the official grpc, but users do not have to have too much psychological burden, it is very troublesome to install so many dependencies at once, goctl has already helped you achieve it, you only need to use goctl rpc protoc will automatically detect dependencies and install them when generating code.

In summary, it is recommended that users who are still using this command replace it with goctl rpc protoc as soon as possible.

3. Why does it always prompt The system cannot find the path specified similar error when generating api/zrpc code on Windows?

 goctl.exe api go -api test.api -dir .
Stat : The system cannot find the path specified.

Description: The reason for this is because go list -json -m get a fixed value command-line-arguments when getting the go module, the problem has been in https://github.com/zeromicro/go-zero/pull /1897 will be fixed, and the version will take effect in v1.3.6 .

4. No help topic 'proto'

Description: This command has been removed in v1.3.4 goctl rpc protoc and replaced with ---2d55eddc17fe3d99816eaec25dbf8cfa---

5. Go_package error when zrpc generates code

 protoc-gen-go: unable to determine Go import path for "greet.proto"

Please specify either:
• a "go_package" option in the .proto source file, or
• a "M" argument on the command line.

See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

--go_out: protoc-gen-go: Plugin failed with status code 1.

6. When zrpc generates code, the output directory of pb is specified as the current service main directory

 the output of pb.go and _grpc.pb.go must not be the same with --zrpc_out:
pb output: /Users/keson/workspace/awesome-goctl/zwesome-zrpc/pb_in_main
zrpc out: /Users/keson/workspace/awesome-goctl/zwesome-zrpc/pb_in_main

7. Why does my code generate pb client (not) with the client flag?

This is an old version goctl rpc proto which only had this problem when generating zrpc code, and this instruction has been removed.

8. Why is my generated directory structure different from the documentation demo?

For goctl rpc protoc the directory structure of zrpc code generation, there will be no difference in the overall structure, the only difference is the output directory of pb, which depends on the parameters you specify to control, control the pb output directory The factors are go_opt , go-grpc_opt , go_package , please refer to " Protocol Buffers: Go Generated Code " for details.

9. Why I installed the goctl editor plugin but still can't highlight

Open Goland's settings, search for FileTypes and set the api file suffix.

project address

https://github.com/zeromicro/go-zero

Welcome go-zero and star support us!

WeChat exchange group

Follow the official account of " Microservice Practice " and click on the exchange group to get the QR code of the community group.


kevinwan
931 声望3.5k 粉丝

go-zero作者