在VSC中为Golang源代码文件添加运行操作按钮的方法
在VSC中为Golang源代码文件添加运行操作按钮的方法

3 回复
我正在使用VSC网页上的官方操作按钮示例,但似乎存在一个错误。
更多关于在VSC中为Golang源代码文件添加运行操作按钮的方法的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
只是一张截图?你的问题是什么?
在VSCode中为Go源代码文件添加运行按钮可以通过配置launch.json文件实现。以下是具体步骤和示例配置:
- 打开VSCode,进入Debug视图(Ctrl+Shift+D)
- 点击"create a launch.json file",选择Go环境
- 在launch.json中添加以下配置:
{
"version": "0.2.0",
"configurations": [
{
"name": "Run Current File",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${file}"
}
]
}
- 保存后,在Debug视图顶部会出现运行按钮
或者使用tasks.json配置构建任务:
{
"version": "2.0.0",
"tasks": [
{
"label": "Run Go File",
"type": "shell",
"command": "go",
"args": ["run", "${file}"],
"group": "build"
}
]
}
在代码编辑区域右键选择"Run Task"即可执行当前文件。
对于更便捷的操作,可以安装Go扩展并设置快捷键绑定:
// 在keybindings.json中添加
{
"key": "ctrl+shift+r",
"command": "workbench.action.tasks.runTask",
"args": "Run Go File"
}
这样就能通过快捷键或调试面板的按钮直接运行当前Go文件。

