Visual Studio 使用 GO 的代码 - 多个主要声明(启动设置)

Visual Studio Code with GO - Multiple main declarations (launch settings)

我是 VS 代码和 Golang 的新手。
我有一个包含 2 种不同服务的现有项目 - 我们称其为 A,第二个为 B。
A 和 B 都位于同一目录下。

每当我尝试 运行 A 或 B 时,我都会收到以下错误:

# directory/directory/directory/A&B_Directory
./A.go:12:6: main redeclared in this block
    previous declaration at ./B.go:18:6

我尝试使用 launch.json 文件,添加以下部分:

   {
        "name": "Launch Program",
        "type": "go",
        "request": "launch",
        "mode": "debug",
        "program": "FullDirectory/A.go"
    }

还尝试在程序属性中设置为 ${file} 和许多其他失败的变体。

我很想知道一些方向,我有点迷路了。 谢谢。

免责声明这不是推荐的方法,我同意其他人的意见,你应该分开服务 A和B放到不同的目录下。

回答您的问题:要启动特定文件,请使用以下配置来模拟 go run current-file

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Run current file",
            "type": "go",
            "request": "launch",
            "mode": "exec",
            "program": "full-path-to-go.exe",
            "args": ["run", "${file}"],
            "showLog": true
        }
    ]
}

模式 exec 用于启动 属性 program 中给出的预构建二进制文件(您必须指定 go 二进制文件的完整路径)。然后作为参数,只需将 run 和文件名 (${file}) 添加到 属性 args