Author: A peck debugIT

preface

Go-zero is a Web and RPC framework that integrates various engineering practices. The stability of large concurrent server is guaranteed by elastic design, which has been fully tested in practice.

Go-zero includes a minimalist API definition and generation tool called GoCTL, which can generate go, iOS, Android, Kotlin, Dart, TypeScript, and JavaScript code from defined API files in one click and run directly.

Why don’t you want to give it a try? !

Go language environment construction

Configure the GO language environment

Add go agent

GOPROXY=https://goproxy.io,direct,https://mirrors.aliyun.com/goproxy/,https://goproxy.cn,https://athens.azurefd.net,http s://gonexus.dev

Setting environment Variables

To the environment variable path%GOROOT%\bin; %GOPATH%\bin

Configuration in vscode

Installing a plug-in

The key is the configuration of the go language environment in vscode, as follows:

{
    "go.goroot": "D:\\go"."go.gopath": "D:\\go_project"."go.inferGopath": false."go.toolsGopath": "D:\\go_project"."window.zoomLevel": 0."git.autofetch": true."terminal.integrated.shell.windows": "powershell.exe", // You can also use cmd.exe"workbench.colorTheme": "Monokai Pro (Filter Machine)"."workbench.iconTheme": "Monokai Pro (Filter Machine) Icons"."editor.renderControlCharacters": false."editor.snippetSuggestions": "top"."editor.suggest.snippetsPreventQuickSuggestions": true."breadcrumbs.enabled": true."terminal.explorerKind": "external"."editor.cursorStyle": "block"."editor.links": false."editor.mouseWheelZoom": true."editor.renderLineHighlight": "all"."editor.suggest.shareSuggestSelections": true."outline.icons": true."search.showLineNumbers": true."search.smartCase": true, // package lookup mode"go.gocodePackageLookupMode": "go"."go.gotoSymbol.includeGoroot": true."go.gotoSymbol.includeImports": true, // build related"go.buildOnSave": "off"."go.gocodeAutoBuild": true."go.installDependenciesWhenBuilding": true."go.buildFlags": []."go.buildTags": ""."go.coverOnSingleTest": true."go.useCodeSnippetsOnFunctionSuggest": true."go.useCodeSnippetsOnFunctionSuggestWithoutType": true."go.docsTool": "guru"."go.formatTool": "goimports"."go.lintTool": "golangci-lint"."go.lintOnSave": "package"."go.lintFlags": [
        "--fast"]."go.formatFlags": []."go.vetFlags": []."go.vetOnSave": "package"."go.generateTestsFlags": []."go.liveErrors": {
        "enabled": true."delay": 500}."go.gocodeFlags": [
        "-builtin"."-ignore-case"."-unimported-packages"]."go.enableCodeLens": {
        "references": true."runtest": true
    },
    "go.delveConfig": {
        "dlvLoadConfig": {
            "followPointers": true."maxVariableRecurse": 1,
            "maxStringLen": 64,
            "maxArrayValues": 64,
            "maxStructFields": 1},"apiVersion": 2."showGlobalVariables": true
    },
    "go.editorContextMenuCommands": {
        "toggleTestFile": true."addTags": true."removeTags": true."testAtCursor": true."testFile": true."testPackage": true."generateTestForFunction": true."generateTestForFile": true."generateTestForPackage": true."addImport": true."testCoverage": true."playground": true."debugTestAtCursor": true
    },
    "go.playground": {
        "openbrowser": false."share": false."run": false
    },
    "go.addTags": {
        "tags": "json"."options": "json=omitempty"."promptForTags": true."transform": "snakecase"
    },
    "go.removeTags": {
        "tags": ""."options": ""."promptForTags": false
    },
    "[go]": {
        "editor.codeActionsOnSave": {
            "source.organizeImports": true}},"go.alternateTools": {
        "go-langserver": "gopls",},"go.useLanguageServer": false."go.languageServerFlags": []."go.languageServerExperimentalFeatures": {
        "format": true."autoComplete": true."rename": true."goToDefinition": true."hover": true."signatureHelp": true."goToTypeDefinition": true."goToImplementation": true."documentSymbols": true."workspaceSymbols": true."findReferences": true."diagnostics": false}}Copy the code

Refer to the link: www.cnblogs.com/chnmig/p/10…

Cloning go – zero

Github address: github.com/tal-tech/go…

Learn the video at talkgo.org/t/topic/729

Documentation: www.yuque.com/tal-tech/go…

Play goctl tools

Goctl is a code generation tool under the Go-Zero microservices framework that can quickly improve development efficiency and allow developers to focus their time on business coding…

Command not found: goctl error

I also installed the goctl tool in the $GOPATH/bin directory

? x-oss-

$GOPATH/bin = $GOPATH/bin = $GOPATH/bin = $GOPATH/bin After joining, restart the computer, and the goctl tool can be used normally.

Next, experiment with the Goctl tool.

Experience the goctl tool

C:\Users\domin>d:

D:\>cd D:\go-learn

D:\go-learn>goctl api new greet
[32mDone.[0m

D:\go-learn>cd greet

D:\go-learn\greet>go mod init
go mod init: go.mod already exists

D:\go-learn\greet>go mod tidy
go: finding module for package github.com/tal-tech/go-zero/core/logx
go: finding module for package github.com/tal-tech/go-zero/rest
go: finding module for package github.com/tal-tech/go-zero/rest/httpx
go: finding module for package github.com/tal-tech/go-zero/core/conf
go: found github.com/tal-tech/go-zero/core/conf in github.com/tal-tech/go-zero v1.1.2
go: found github.com/tal-tech/go-zero/rest in github.com/tal-tech/go-zero v1.1.2
go: found github.com/tal-tech/go-zero/rest/httpx in github.com/tal-tech/go-zero v1.1.2
go: found github.com/tal-tech/go-zero/core/logx ingithub.com/tal-tech/go-zero v1.2d :\go-learn\greet> Go run greet.go -f etc/greet-api.yaml Starting server at 0.0.0.0:8888... {"@timestamp":"The 2021-01-10 T01:04:05. 746 + 08"."level":"stat"."content":"CPU: 0m, MEMORY: Alloc= 0.6mi, TotalAlloc= 0.6mi, Sys= 0.6mi, NumGC=0"}
{"@timestamp":"The 2021-01-10 T01:04:05. 751 + 08"."level":"stat"."content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"The 2021-01-10 T01:05:05. 747 + 08"."level":"stat"."content":"CPU: 0m, MEMORY: Alloc= 0.6mi, TotalAlloc= 0.6mi, Sys= 0.6mi, NumGC=0"}
{"@timestamp":"The 2021-01-10 T01:05:05. 751 + 08"."level":"stat"."content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"The 2021-01-10 T01:06:05. 746 + 08"."level":"stat"."content":"CPU: 0m, MEMORY: Alloc= 0.6mi, TotalAlloc= 0.6mi, Sys= 0.6mi, NumGC=0"}
{"@timestamp":"The 2021-01-10 T01:06:05. 750 + 08"."level":"stat"."content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"The 2021-01-10 T01:07:05. 746 + 08"."level":"stat"."content":"CPU: 0m, MEMORY: Alloc= 0.6mi, TotalAlloc= 0.6mi, Sys= 0.6mi, NumGC=0"}
{"@timestamp":"The 2021-01-10 T01:07:05. 752 + 08"."level":"stat"."content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"The 2021-01-10 T01:08:05. 744 + 08"."level":"stat"."content":"CPU: 0m, MEMORY: Alloc= 0.6mi, TotalAlloc= 0.6mi, Sys= 0.6mi, NumGC=0"}
{"@timestamp":"The 2021-01-10 T01:08:05. 750 + 08"."level":"stat"."content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"The 2021-01-10 T01:09:05. 746 + 08"."level":"stat"."content":"CPU: 0m, MEMORY: Alloc= 0.6mi, TotalAlloc= 0.6mi, Sys= 0.6mi, NumGC=0"}
{"@timestamp":"The 2021-01-10 T01:09:05. 750 + 08"."level":"stat"."content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
Copy the code

Open another CMD window

C:\Users\domin>d:

D:\>cdD:\go-learn\greet D:\go-learn\greet>goctl api java -api greet.api -dir greet [32mDone.[0m D:\go-learn\greet>curl -i http://localhost:8888/from/you HTTP / 1.1 200 OK the content-type: application/json Date: Sat, 09 Jan 2021 17:09:06 GMT Content-Length: 14Copy the code

Generated code

conclusion

In addition to the Goctl artifact, Go-Zero also has a number of gadgets.

  1. Stream data processing tool: FX. Such as java8 lambda, go-zero also has! Fx.filter ().sort ().head (), which simplifies the complex processing of arrays.
  2. List itemmapReduce reduces service latency: Mr. Finish(), Mr. Map().reduce (), say goodbye to the concurrent processing waitGroup.
  3. Integration of ETCD service discovery: P2C algorithmic discovery services, avoid developers point-to-point or nginx forwarding services, install an ETCD and be done.
  4. JWT integration API: Easily have a JWT backend service.
  5. Integration with Prometheus: Easily have a Golang backend service with monitoring.

Go language is mainly used for server-side development, which is positioned to develop “large software”. It is suitable for many programmers to develop large software together, and the development cycle is long, and supports cloud computing network services. The Go language allows programmers to develop quickly, and it makes it easier to maintain and modify software as it grows. It combines the high efficiency of traditional compiled languages with the ease of use and expressiveness of scripting languages.

As a server programming language, Go language is very suitable for processing logs, data packaging, virtual machine processing, file system, distributed system, database agent, etc. In network programming, Go language is widely used in Web applications, API applications, download applications, etc. In addition, Go language can also be used in the field of in-memory database and cloud platform, at present, many foreign cloud platforms are using Go development.

Refer to the link: www.jianshu.com/p/6fbba7f7c…

Go-zero address: github.com/tal-tech/go…