Windows下如何玩轉火熱的go-zero

做者:阿啄debugIThtml

前言

go-zero 是一個集成了各類工程實踐的 web 和 rpc 框架。經過彈性設計保障了大併發服務端的穩定性,經受了充分的實戰檢驗。java

go-zero 包含極簡的 API 定義和生成工具 goctl,能夠根據定義的 api 文件一鍵生成 Go, iOS, Android,
Kotlin, Dart, TypeScript, JavaScript 代碼,並可直接運行。nginx

這麼牛,怎麼不想去試一下?!git

go語言環境搭建

配置GO語言環境

新增go的代理程序員

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

設置環境變量

環境變量path中添加 %GOROOT%\bin;%GOPATH%\bin
imagegolang

在vscode的配置

安裝插件
imageweb

關鍵是go語言環境在vscode的配置,以下:算法

{
    "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
    "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
    }
}

參考連接:http://www.javashuo.com/article/p-vepencgo-oa.htmlshell

克隆go-zero

github地址:https://github.com/tal-tech/go-zero

學習視頻地址:https://talkgo.org/t/topic/729

語雀文檔資料:https://www.yuque.com/tal-tech/go-zero/tdsgkg

image

玩goctl工具

goctl是go-zero微服務框架下的代碼生成工具,其能夠快速提高開發效率,讓開發人員將時間重點放在業務coding上……

查詢不少資料都不說的,不清晰,老是報command not found: goctl錯誤。

我也是將goctl工具安裝到 $GOPATH/bin 目錄下

image?x-oss-

環境變量也配置了,最後發現,全部網上博客都沒有說,把 $GOPATH/bin,加入系統環境path變量。加入之後,重啓電腦,就能夠正常使用goctl工具。

image

接下來,就是體驗goctl工具。

體驗goctl工具

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"}

image

再另外開一個cmd窗口

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

image

生成的代碼

image

總結

除了goctl神器,go-zero還有不少的小工具。

  1. 流數據處理利器:fx。如java8的lambda,,go-zero也有了!fx.Filter().Sort().Head() ,讓數組的複雜處理變得簡單。
  2. List itemmapReduce下降服務相應時間:mr.Finish(), mr.Map().Reduce(), 跟併發處理waitGroup說拜拜。
  3. etcd服務發現的集成:p2c的算法發現服務,避免開發人員點對點或nginx的轉發服務,安裝一個etcd就完事了。
  4. jwt集成api:輕鬆擁有一個jwt的後臺服務。
  5. 集成Prometheus:輕鬆擁有一個帶監控的golang後臺服務。

Go語言主要用做服務器端開發,其定位是用來開發「大型軟件」的,適合於不少程序員一塊兒開發大型軟件,而且開發週期長,支持雲計算的網絡服務。Go語言可以讓程序員快速開發,而且在軟件不斷的增加過程當中,它能讓程序員更容易地進行維護和修改。它融合了傳統編譯型語言的高效性和腳本語言的易用性和富於表達性。

Go語言做爲服務器編程語言,很適合處理日誌、數據打包、虛擬機處理、文件系統、分佈式系統、數據庫代理等;網絡編程方面,Go語言普遍應用於Web應用、API應用、下載應用等;除此以外,Go語言還可用於內存數據庫和雲平臺領域,目前國外不少雲平臺都是採用Go開發。

參考連接:https://www.jianshu.com/p/6fbba7f7ced3

go-zero地址:https://github.com/tal-tech/go-zero

go-zero 系列文章見『微服務實踐』公衆號

相關文章
相關標籤/搜索