Go_vscode玩转golang

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangxuan0261/article/details/81810416

Go_vscode玩转golang


环境配置

vscode 配置 go 及其 下载失败解决办法: https://blog.csdn.net/Yo_oYgo/article/details/79065966

配置好把所有的测试代码都丢到一个测试目录下, 比如 GOPATH/src/GoLab (GOPATH 为项目路径)


工作区配置

    "go.buildOnSave": "off", //在保存代码时自动编译代码
    "go.lintOnSave": "off", //在保存代码时自动检查代码可以优化的地方,并给出建议
    "go.lintTool": "golint", //使用 golint 库来执行 lint操作,你也可以选择使用gometalinter
    "go.vetOnSave": "off",
    "go.gocodeAutoBuild": true, //代码自动编译构建
    "go.buildFlags": [],
    "go.lintFlags": [],
    "go.vetFlags": [],
    "go.testFlags": ["-v"],
    "go.formatTool": "goreturns",
    "go.goroot": "D:/go",
    "go.gopath": "${workspaceRoot}",
    "go.autocompleteUnimportedPackages": true,
    "go.gocodePackageLookupMode": "go",
    "go.gotoSymbol.includeImports": true,
    "go.useCodeSnippetsOnFunctionSuggest": true, //使用代码片段作为提示
    "go.inferGopath": true,
    "go.useCodeSnippetsOnFunctionSuggestWithoutType": true,
    "go.liveErrors": {
        "enabled": true,
        "delay": 0
    },

vscode 实时报错提示

settings.json 中加入配置

    "go.liveErrors": {
        "enabled": true,
        "delay": 500
    }

然后第一次会提示安装 gotype-live, 安装即可

然后重新打开工作区, 每次保存文件都会实时提示报错


单个文件运行

  1. 测试文件 test_pkg.go 的必须 包名是main, 且有 main 方法

    // package test_pkg
    package main
    
    import (
    "fmt"
    )
    
    func main() {
    fmt.Println("hello world")
    }
  2. vscode 写个任务 ( .vscod/tasks.json ) , 调用 go 指令 run 当前文件, 方便快速运行, 快捷键 ctrl + shift + b

    {
       "version": "2.0.0",
       "tasks": [
           {
               "label": "run",
               "type": "shell",
               "presentation": {
                   "echo": true,
                   "reveal": "always",
                   "focus": true,
                   "panel": "shared"
               },
               "command": "go",
               "args": [
                   "run",
                   "${file}"
               ],
               "group": {
                   "kind": "build",
                   "isDefault": true
               },
               "problemMatcher": []
           }
       ]
    }

调试一个go程序

假设有一段测试程序在 GOPATH/src/GoLab 下, 且目录结构如下

扫描二维码关注公众号,回复: 2960284 查看本文章
  1. 假设需要测试的入口文件是 test_pkg.go. 必须 包名是main, 且有 main 方法

    // package test_pkg
    package main
    
    import (
    pkg001 "GoLab/test_pkg/pkg001"
    pkg002 "GoLab/test_pkg/pkg002"
    "fmt"
    )
    
    func main() {
    pkg001.SayHello()
    pkg002.SayHello222()
    
    d1 := &pkg002.Dog{"123", 21}
    d2 := *d1
    fmt.Println("d1:", d1)
    fmt.Println("d2:", d2)
    }
  2. vscode 调试配置 ( .vscod/launch.json )

    {
       "version": "0.2.0",
       "configurations": [
    
           {
               "name": "mytest",
               "type": "go",
               "request": "launch",
               "mode": "debug",
               "remotePath": "",
               // "port": 10550,
               // "host": "127.0.0.1",
               "program": "${workspaceRoot}/src/GoLab/test_pkg/test_pkg.go", // 指定入口文件
               "env": {
                   "GOPATH":"${workspaceRoot}" // 设置项目路径, 就不会去取系统环境变量中的GOPATH变量值
               },
               "args": [],
               "showLog": true
           }
       ]
    }
  3. 快捷键 f5 调试程序


leaf框架

GitHub : https://github.com/name5566/leaf

中文文档: https://github.com/name5566/leaf/blob/master/TUTORIAL_ZH.md


mqant框架

GitHub : https://github.com/liangdas/mqant

中文文档: https://github.com/liangdas/mqant/wiki/images/mqant_architecture.png

社区 : http://www.mqant.com

使用


后记

看了上面两个 GitHub 上 start 比较高开源项目, 花了简单除暴的流程图方便记忆

  • mqant

    比较偏爱分布式框架, 流程也比 leaf 单进程部署框架 稍微复杂点

  • leaf

猜你喜欢

转载自blog.csdn.net/yangxuan0261/article/details/81810416