DotnetProg
DotnetProg

Reputation: 810

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

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.

Upvotes: 1

Views: 7671

Answers (1)

putu
putu

Reputation: 6444

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.

Upvotes: 10

Related Questions