Build the go language development environment under window10, experience the go-zero framework, and solve command not found: goctl

Preface

go-zero is a web and rpc framework that integrates various engineering practices. The stability of the large concurrent server is guaranteed through flexible design, and it has withstood full actual combat tests.

go-zero includes a minimalist API definition and generation tool goctl, which can generate Go, iOS, Android, Kotlin, Dart, TypeScript, JavaScript code with one click according to the defined api file, and run it directly.

So awesome, why don't you want to try it? !

Go language environment construction

Configure GO locale

新增go的代理 GOPROXY=https://goproxy.io,direct,https://mirrors.aliyun.com/goproxy/,https://goproxy.cn,https://athens.azurefd.net,https://gonexus.dev

Set environment variables

Add %GOROOT%\bin;%GOPATH%\bin to the environment variable path

Configuration in vscode

Install plugin

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",	// 也可以使用 cmd.exe
    // "terminal.integrated.shellArgs.windows": [
    //     "/k",
    //     "D:\\Applications\\Cmder\\Cmder.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 查找模式
    "go.gocodePackageLookupMode": "go",
    "go.gotoSymbol.includeGoroot": true,
    "go.gotoSymbol.includeImports": true,

    // build 相关
    "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
    }
}

Excellent link: https://www.cnblogs.com/chnmig/p/10796316.html

Katsutaka go-zero

Domestic gitee address: https://gitee.com/person_quickly_work/go-zero

Foreign github address: https://github.com/tal-tech/go-zero

Learning video address: https://talkgo.org/t/topic/729

Yuque documentation: https://www.yuque.com/tal-tech/go-zero/tdsgkg

Play with goctl tool

Goctl is a code generation tool under the go-zero microservice framework. It can quickly improve development efficiency and allow developers to focus their time on business coding...

I didn't mention many information in the query, it was not clear, and always reported command not found: goctl error.

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

The environment variables are also configured, and finally found that all online blogs did not say that $GOPATH/bin was added to the system environment path variable. After joining, restart the computer and you can use the goctl tool normally.

Next, is to experience the goctl tool.

Experience 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 in github.com/tal-tech/go-zero v1.1.2

D:\go-learn\greet>go run greet.go -f etc/greet-api.yaml
Starting server at 0.0.0.0:8888...
{"@timestamp":"2021-01-10T01:04:05.746+08","level":"stat","content":"CPU: 0m, MEMORY: Alloc=0.6Mi, TotalAlloc=0.6Mi, Sys=6.6Mi, NumGC=0"}
{"@timestamp":"2021-01-10T01:04:05.751+08","level":"stat","content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"2021-01-10T01:05:05.747+08","level":"stat","content":"CPU: 0m, MEMORY: Alloc=0.6Mi, TotalAlloc=0.6Mi, Sys=6.6Mi, NumGC=0"}
{"@timestamp":"2021-01-10T01:05:05.751+08","level":"stat","content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"2021-01-10T01:06:05.746+08","level":"stat","content":"CPU: 0m, MEMORY: Alloc=0.6Mi, TotalAlloc=0.6Mi, Sys=6.6Mi, NumGC=0"}
{"@timestamp":"2021-01-10T01:06:05.750+08","level":"stat","content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"2021-01-10T01:07:05.746+08","level":"stat","content":"CPU: 0m, MEMORY: Alloc=0.6Mi, TotalAlloc=0.6Mi, Sys=6.6Mi, NumGC=0"}
{"@timestamp":"2021-01-10T01:07:05.752+08","level":"stat","content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"2021-01-10T01:08:05.744+08","level":"stat","content":"CPU: 0m, MEMORY: Alloc=0.6Mi, TotalAlloc=0.6Mi, Sys=6.6Mi, NumGC=0"}
{"@timestamp":"2021-01-10T01:08:05.750+08","level":"stat","content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"2021-01-10T01:09:05.746+08","level":"stat","content":"CPU: 0m, MEMORY: Alloc=0.6Mi, TotalAlloc=0.6Mi, Sys=6.6Mi, NumGC=0"}
{"@timestamp":"2021-01-10T01:09:05.750+08","level":"stat","content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}

Open another cmd window

C:\Users\domin>d:

D:\>cd D:\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
Content-Type: application/json
Date: Sat, 09 Jan 2021 17:09:06 GMT
Content-Length: 14

Generated code

to sum up

In addition to the goctl artifact, go-zero also has many small tools.

  1. Streaming data processing tool: fx. Such as lambda of java8, go-zero is also available! fx.Filter().Sort().Head(), makes the complex processing of the array simple.
  2. mapReduce reduces the service response time: mr.Finish(), mr.Map().Reduce(), say goodbye to the concurrent processing waitGroup.
  3. Integration of etcd service discovery: p2c algorithm discovery service, avoiding developers' point-to-point or nginx forwarding service, install an etcd and it's done.
  4. Jwt integrated api: easy to have a jwt background service.
  5. Integrate Prometheus: easily have a golang background service with monitoring.

Go language is mainly used for server-side development, and its positioning is to develop "large-scale software". It is suitable for many programmers to develop large-scale software together, and has a long development cycle and supports cloud computing network services. Go language allows programmers to develop quickly, and in the process of continuous growth of software, it allows programmers to more easily maintain and modify. It combines the 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 systems, distributed systems, database agents, etc.; in terms of network programming, Go language is widely used in web applications, API applications, download applications, etc.; In addition, the Go language can also be used in the field of memory databases and cloud platforms. At present, many cloud platforms abroad are developed using Go.

Excellent link: https://www.jianshu.com/p/6fbba7f7ced3

Guess you like

Origin blog.csdn.net/as4589sd/article/details/112464882