溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Windows中g(shù)o-zero如何使用

發(fā)布時(shí)間:2021-08-13 13:44:41 來源:億速云 閱讀:262 作者:Leah 欄目:編程語言

本篇文章為大家展示了Windows中g(shù)o-zero如何使用,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

go語言環(huán)境搭建

配置GO語言環(huán)境

新增go的代理

GOPROXY=https://goproxy.io,direct,https://mirrors.aliyun.com/goproxy/,https://goproxy.cn,https://athens.azurefd.net,https://gonexus.dev Windows中g(shù)o-zero如何使用

設(shè)置環(huán)境變量

環(huán)境變量path中添加 %GOROOT%\bin;%GOPATH%\bin Windows中g(shù)o-zero如何使用

在vscode的配置

安裝插件 Windows中g(shù)o-zero如何使用

關(guān)鍵是go語言環(huán)境在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 相關(guān)
    "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

學(xué)習(xí)視頻地址:https://talkgo.org/t/topic/729

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

Windows中g(shù)o-zero如何使用

玩goctl工具

goctl是go-zero微服務(wù)框架下的代碼生成工具,其可以快速提升開發(fā)效率,讓開發(fā)人員將時(shí)間重點(diǎn)放在業(yè)務(wù)coding上……

查詢很多資料都不說的,不清晰,總是報(bào)command not found: goctl錯(cuò)誤。

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

Windows中g(shù)o-zero如何使用?x-oss-

環(huán)境變量也配置了,最后發(fā)現(xiàn),所有網(wǎng)上博客都沒有說,把 $GOPATH/bin,加入系統(tǒng)環(huán)境path變量。加入以后,重啟電腦,就可以正常使用goctl工具。

Windows中g(shù)o-zero如何使用

接下來,就是體驗(yàn)goctl工具。

體驗(yàn)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中g(shù)o-zero如何使用

再另外開一個(gè)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中g(shù)o-zero如何使用

生成的代碼

Windows中g(shù)o-zero如何使用

總結(jié)

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

  1. 流數(shù)據(jù)處理利器:fx。如java8的lambda,,go-zero也有了!fx.Filter().Sort().Head() ,讓數(shù)組的復(fù)雜處理變得簡單。

  2. List itemmapReduce降低服務(wù)相應(yīng)時(shí)間:mr.Finish(), mr.Map().Reduce(), 跟并發(fā)處理waitGroup說拜拜。

  3. etcd服務(wù)發(fā)現(xiàn)的集成:p2c的算法發(fā)現(xiàn)服務(wù),避免開發(fā)人員點(diǎn)對(duì)點(diǎn)或nginx的轉(zhuǎn)發(fā)服務(wù),安裝一個(gè)etcd就完事了。

  4. jwt集成api:輕松擁有一個(gè)jwt的后臺(tái)服務(wù)。

  5. 集成Prometheus:輕松擁有一個(gè)帶監(jiān)控的golang后臺(tái)服務(wù)。

上述內(nèi)容就是Windows中g(shù)o-zero如何使用,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI