Windows中go-zero如何使用

本文主要介绍"Windows中go-zero怎么使用",希望能够解决您遇到有关问题,下面我们一起来看这篇 "Windows中go-zero怎么使用" 文章。

go语言环境搭建

配置GO语言环境

新增go的代理

GOPROXY=https://goproxy.io,direct,https://mirrors.aliyun.com/goproxy/,https://goproxy.cn,https://athens.azurefd.net,https://gonexus.dev Windows中go-zero如何使用

设置环境变量

环境变量path中添加 %GOROOT%\bin;%GOPATH%\bin Windows中go-zero如何使用

在vscode的配置

安装插件 Windows中go-zero如何使用

关键是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

Windows中go-zero如何使用

玩goctl工具

goctl是go-zero微服务框架下的代码生成工具,其可以快速提升开发效率,让开发人员将时间重点放在业务coding上……

查询很多资料都不说的,不清晰,总是报command not found: goctl错误。

我也是将goctl工具安装到 $GOPATH/bin 目录下

Windows中go-zero如何使用?x-oss-

环境变量也配置了,最后发现,所有网上博客都没有说,把 $GOPATH/bin,加入系统环境path变量。加入以后,重启电脑,就可以正常使用goctl工具。

Windows中go-zero如何使用

接下来,就是体验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"}

Windows中go-zero如何使用

再另外开一个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

Windows中go-zero如何使用

生成的代码

Windows中go-zero如何使用

总结

除了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后台服务。

关于 "Windows中go-zero怎么使用" 就介绍到这。希望大家多多支持编程宝库

如何使用JVM故障处理工具:本文主要介绍"怎么使用JVM故障处理工具",希望能够解决您遇到有关问题,下面我们一起来看这篇 "怎么使用JVM故障处理工具" 文章。一、前言用都用不到怎么学?没有场景、没有诉求,怎么学习这些似乎用不上知识点。其实 ...