作者:阿啄debugIT
前言
go-zero 是一個集成了各種工程實踐的 web 和 rpc 框架,通過彈性設計保障了大并發服務端的穩定性,經受了充分的實戰檢驗,
go-zero 包含極簡的 API 定義和生成工具 goctl,可以根據定義的 api 檔案一鍵生成 Go, iOS, Android,
Kotlin, Dart, TypeScript, JavaScript 代碼,并可直接運行,
這么牛,怎么不想去試一下?!
go語言環境搭建
配置GO語言環境
新增go的代理
GOPROXY=https://goproxy.io,direct,https://mirrors.aliyun.com/goproxy/,https://goproxy.cn,https://athens.azurefd.net,https://gonexus.dev

設定環境變數
環境變數path中添加 %GOROOT%\bin;%GOPATH%\bin

在vscode的配置
安裝插件

關鍵是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
}
}
參考鏈接:https://www.cnblogs.com/chnmig/p/10796316.html
克隆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

玩goctl工具
goctl是go-zero微服務框架下的代碼生成工具,其可以快速提升開發效率,讓開發人員將時間重點放在業務coding上……
查詢很多資料都不說的,不清晰,總是報command not found: goctl錯誤,
我也是將goctl工具安裝到 $GOPATH/bin 目錄下
?x-oss-
環境變數也配置了,最后發現,所有網上博客都沒有說,把 $GOPATH/bin,加入系統環境path變數,加入以后,重啟電腦,就可以正常使用goctl工具,

接下來,就是體驗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"}

再另外開一個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

生成的代碼

總結
除了goctl神器,go-zero還有很多的小工具,
- 流資料處理利器:fx,如java8的lambda,,go-zero也有了!fx.Filter().Sort().Head() ,讓陣列的復雜處理變得簡單,
- List itemmapReduce降低服務相應時間:mr.Finish(), mr.Map().Reduce(), 跟并發處理waitGroup說拜拜,
- etcd服務發現的集成:p2c的演算法發現服務,避免開發人員點對點或nginx的轉發服務,安裝一個etcd就完事了,
- jwt集成api:輕松擁有一個jwt的后臺服務,
- 集成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 系列文章見『微服務實踐』公眾號
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/248840.html
標籤:Go
