关于前端:windows下如何玩转火热的gozero

6次阅读

共计 6795 个字符,预计需要花费 17 分钟才能阅读完成。

前言

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
    },
    "": {"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/chnmi…

克隆 go-zero

github 地址:https://github.com/tal-tech/g…

学习视频地址:https://talkgo.org/t/topic/729

语雀文档资料:https://www.yuque.com/tal-tec…

玩 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 还有很多的小工具。

  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

本篇文章由一文多发平台 ArtiPub 主动公布

正文完
 0