使用GO的Visual Studio Code – 多个main声明(启动设置)

huangapple go评论97阅读模式
英文:

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

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对VS Code和Golang都不太熟悉。
我有一个包含两个不同服务的现有项目,我们称之为A和B。
A和B都位于同一个目录下。

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

# directory/directory/directory/A&B_Directory
./A.go:12:6: main在此块中重新声明
	之前的声明在./B.go:18:6中

我尝试修改launch.json文件,添加了以下部分:

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

我还尝试将program属性设置为${file}和其他许多失败的变体。

我希望能得到一些指导,我有点迷茫。
谢谢。

英文:

I'm new to VS code and Golang.
I have an existing project containing 2 different services - let's call one A and the second one B.
Both A and B sits under the same directory.

Whenever I try to run A or B, I get the following error :

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

I tried playing with the launch.json file, adding the following sections :

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

Also tried in the program attribute to set to ${file} and many other variations that failed.

I'd love for some direction, I'm kinda lost.
Thanks.

答案1

得分: 10

免责声明:这不是推荐的方法,我同意其他人的观点,你应该将服务A和B分别放在不同的目录中。

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

{
	"version": "0.2.0",
	"configurations": [
		{
			"name": "运行当前文件",
			"type": "go",
			"request": "launch",
			"mode": "exec",
			"program": "go.exe的完整路径",
			"args": ["run", "${file}"],
			"showLog": true
		}
	]
}

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

英文:

Disclaimer: this is not the recommended approach, I agree with others, you shall separate the service A and B into different directory.

Answer to your question: To launch a specific file, use the following configuration to emulate 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
		}
	]
}

Mode exec is for launching pre-built binary given in the property program (you must specify full path to go binary). Then as the arguments, just add run and filename (${file}) to property args.

huangapple
  • 本文由 发表于 2017年8月30日 20:56:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/45961085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定