vscode开发golang配置

1、安装vscode

去官网下载,如果下载速度慢的话,直接将下载文件地址的主域替换为:http://vscode.cdn.azure.cn

2、安装插件

在界面最左侧的竖行活动栏中点击扩展(command+shift+x)功能,在扩展商店搜索并安装如下插件:

  • Activitus Bar (在底部状态栏上添加活动栏,然后就可以关闭左侧活动栏了,支持自定义其他扩展功能)
  • Chinese (Simplified) (简体中文) Language Pack for Visual Studio Code
  • Code Runner
  • Code Spell Checker
  • Create tests
  • EditorConfig for VS Code (统一编辑器的编码行为)
  • Fix VSCode Checksums
  • Git History
  • GitLens — Git supercharged
  • LeetCode (另需安装nodejs)
  • Local History
  • Markdown All in One
  • Material Icon Theme
  • One Dark Pro
  • Rainbow Brackets
  • REST Client
  • Todo Tree
  • Visual Studio IntelliCode
  • Bracket Pair Colorizer 2

3、开发环境准备

  • 安装Golang,配置必要的代理,查看:GOPROXY.IO – A Global Proxy for Go Modules
  • 在vscode中使用command+shift+p键打开快捷命令框,输入 >install/Update Tools (输入install就会提示出来)点击此项后,会显示列表:
vscode开发golang配置插图1

选择全部,确定安装。

  • 统一编码规范,比如tab、空格、换行问题。可以采用:EditorConfig,支持多种编辑器和IDE,在工作区内(或项目内)新建.editorconfig文件,写入:
root=true[*]charset=utf-8indent_style=spaceindent_size=4end_of_line=lfinsert_final_newline=truetrim_trailing_whitespace=true

vscode默认的编码行为就确定了。

  • JetBrains Mono字体,地址:JetBrains Mono: A free and open source typeface for developers | JetBrains: Developer Tools for Professionals and Teams。下载后导入字体即可。
  • 熟悉vscode快捷键,在设置中打开键盘快捷方式,可以查看到所有vscode和扩展支持的命令和操作,支持自定义快捷键。
  • 常用command+shift+p,输入扩展程序的名称,就会提示该扩展支持的操作,给结构体添加tag,输入add就会提示Add Tags To Struct Fields, 点击文件中struct会自动添加tag,输入tests根据提示自动生成_test.go文件等等。

4、开发

通过以上准备后,就可以愉快地使用vscode开发golang项目了。

  • vscode支持工作区,同一个工作区内可以添加多个项目,可以更加高效地并行开发。
  • 项目内.vscode文件夹内launch.json是调试项目的配置文件,没有的话可以自行创建;settings.json文件是针对本项目的配置,没有可忽略,使用全局配置。launch.json文件如下:
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name" : "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceRoot}/demo/main.go", //main函数入口
            "buildFlags":"-mod=vendor", // 使用vendor模式
            "env": {"env":"dev"}, // 环境变量
            "args": [] //启动参数
        }
    ]
}
  • command+shift+p 输入settings.json打开用户自定义配置,慢慢琢磨玩吧。附上个人settings.json的配置:
{
    "workbench.colorTheme": "One Dark Pro",
    "workbench.startupEditor": "none",
    "files.autoSave": "afterDelay",
    "workbench.iconTheme": "material-icon-theme",
    "editor.tabSize": 4,
    "editor.fontSize": 13,
    "editor.renderControlCharacters": true,
    "editor.renderWhitespace": "all",
    "editor.detectIndentation": true,
    "editor.insertSpaces": true,
    "go.addTags": {
        "tags": "json",
        "options": "",
        "promptForTags": false,
        "transform": "snakecase",
        "template": ""
    },
    "go.formatTool": "goformat",
    "go.inferGopath": true,
    "go.autocompleteUnimportedPackages": true,
    "go.gocodePackageLookupMode": "go",
    "go.gotoSymbol.includeImports": true,
    "go.useCodeSnippetsOnFunctionSuggest": true,
    "go.useCodeSnippetsOnFunctionSuggestWithoutType": true,
    "go.docsTool": "gogetdoc",
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "json.schemaDownload.enable": false,
    "editor.fontFamily": "JetBrains Mono, Menlo, Monaco, Courier New, monospace",
    "workbench.activityBar.iconClickBehavior": "focus",
    "activitusbar.views": [
        {
            "name": "settings",
            "codicon": "gear"
        },
        {
            "name": "explorer",
            "codicon": "explorer-view-icon"
        },
        {
            "name": "search",
            "codicon": "search-view-icon"
        },
        {
            "name": "scm",
            "codicon": "source-control-view-icon"
        },
        {
            "name": "debug",
            "codicon": "run-view-icon"
        },
        {
            "name": "command.workbench.view.extension.test",
            "codicon": "test-view-icon",
            "tooltip": "Test"
        },
        {
            "name": "extensions",
            "codicon": "extensions-view-icon"
        },
        {
            "name": "command.workbench.view.extension.todo-tree-container",
            "codicon": "book",
            "tooltip": "Todo Tree"
         },
        {
            "name": "command.workbench.view.extension.leetcode",
            "codicon": "rocket",
            "tooltip": "LeetCode"
        },
        {
            "name": "terminal",
            "codicon": "terminal-powershell"
        }
    ],
    "leetcode.endpoint": "leetcode-cn",
    //"leetcode.workspaceFolder": "/xxxx", //算法项目目录
    "leetcode.defaultLanguage": "golang",
    "files.trimTrailingWhitespace": true,
    "explorer.confirmDelete": false,
    "editor.defaultFormatter": "golang.go",
    "editor.formatOnSave": true,
    "go.useLanguageServer": true
}

原创文章 vscode开发golang配置,版权所有
如若转载,请注明出处:https://www.itxiaozhan.cn/202212254.html

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注