使用VSCode作为Golang的IDE

idea过期了,又不想折腾许可证。换个口味,试试VSCode作为Golang的IDE工具。

插件

插件名称 说明
Go GO语言扩展插件
Go Mod Explorer 查看Go依赖包
Git Change Lists — JetBrains IDEs Sync 和idea一样管理git change list的插件

VSCode 全局 settings.json配置

{
    "go.testFlags": [
        "-v",
        "-count=1"
    ], // 禁用测试缓存,每次都是新的执行
    "gopls": {
        "usePlaceholders": true,
        "completeUnimported": true, // 提示未导入的包
        "env": {
        			"GOROOT": "/Users/macbookpro/sdk/go1.25.5/",
             "GOPATH": "/Users/macbookpro/go",
            // "GOPROXY": "http://localhost:1000,direct", 代理
            // "GONOSUMDB": "codeup.aliyun.com",
            "GOSUMDB": "sum.golang.google.cn",
        },
        "directoryFilters": [
            "-**/node_modules",
            "-**/vendor",
            "-**/test",
            "-**/dist",
            "-**/build",
            "-**/.git",
            "-**/.vscode",
            "-**/.idea",
        ], // 目录过滤
        // "formatting.gofumpt": false, // 使用gofumpt增强格式化
        // "formatting.local": "codeup.aliyun.com/ld_group", // 本地包导入分组
        "analyses": {
            "ST1003": false, // 检测无效注释
            // "ST1020": false, // 检测无效注释
            "SA1000": true, // 检测无效正则表达式
            "S1005": true // 检测不必要的空白标识符使用
        },
        "staticcheck": true, // 启用全部staticcheck规则
    },
    "testing.resultsView.layout": "treeLeft",
    "gitlens.graph.minimap.additionalTypes": [
        "localBranches",
        "stashes",
        "pullRequests"
    ],
    "gitlens.locale": "zh",
    "gitlens.defaultDateLocale": "zh",
    "gitlens.defaultDateFormat": "ddd YYYY-MM-DD HH:mm:ss ",
    "gitlens.defaultTimeFormat": "HH:mm:ss",
    "gitlens.defaultDateShortFormat": null,
    "go.formatTool": "gofmt",
    // 调试时显示变量类型信息
    "go.delveConfig": {
        "showGlobalVariables": true
    },
    "gitlens.views.commitDetails.files.layout": "list",
    "gitlens.ai.model": "gitkraken",
    "gitlens.ai.gitkraken.model": "gemini:gemini-2.0-flash",
    "git.openRepositoryInParentFolders": "never",
    "go.languageServerFlags": [],
}

项目内的任务配置tasks.json

{
	"version": "2.0.0",
	"tasks": [
		// 构建
		{
			"type": "go",
			"label": "go: build package",
			"options": {
                "cwd": "${workspaceFolder}"
            },
			"command": "build",
			"args": ["-o", "__debug__main", "main.go"],
			"problemMatcher": [
				"$go"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "cd ${workspaceFolder}; go build"
		}
	]
}

项目内的Debug配置launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Dev Debug Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}",
            "env": {
                "GOROOT": "/Users/macbookpro/sdk/go1.25.5/",
                "GOPATH": "/Users/macbookpro/go",
            },
            "args": [
                "server",
                "--flag1=1",
                "--flag2=2"
            ],
            // "buildFlags": "-ldflags=-s -w",
            "showLog": false,
            // "console": "internalConsole", // 调用默认Debug控制台
            "console": "integratedTerminal", // 调用终端而非Debug控制台,需要交互式输入时,使用这个比较好
        },
        {
            "name": "Exec",
            "type": "go",
            "request": "launch",
            "mode": "exec",
            "program": "${workspaceFolder}/__debug__main",
            "env": {},
            "args": [
                "server",
                "--flag1=1",
                "--flag2=2"
            ],
            "buildFlags": [],
            "preLaunchTask": "go: build package", // 执行前,先执行构建命令
            "postDebugTask": "delete __debug__main"
        }
    ]
}
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐