mornindew
mornindew

Reputation: 2221

Golang Debugger Not Running

I am trying to set up VS Code with Go, and I am getting an error when attaching the debugger. Since I am brand new to VS Code, I have no leads as to why.

I have installed delve debugger:

go get -u github.com/derekparker/delve/cmd/dlv
delv -v 
delv 9.11.3-1ubuntu1.1-Ubuntu

I get this error when I try to attach the debugger to a very simple golang file:

API server listening at: 127.0.0.1:2345
time="2018-08-30T09:39:57-06:00" level=info msg="launching process with args: [/home/craig/Documents/GoLang/src/github.com/mornindew/sharedPackages/email/debug]" layer=debugger
Can not debug non-main package
Process exiting with code: 1

Code:

package email

import "fmt"

// SendEmail - Sends The email
func SendEmail() {

}

func main() {
    fmt.Println("Hello World!")
    i := 101
    fmt.Println(i)
}

This is all very helpful, thank you very much.

It makes me think that I have a problem in my project organization. I have a project that has a bunch of reusable packages. I didn't want to create a github repo for each individual package. Essentially:

package1
-- package1.go
-- package1_test.go
package2
-- package2.go
-- package2_test.go
...
package10
-- package10.go
-- package10_test.go

Is this structured incorrectly? Is there a recommended way to accomplish this?

Upvotes: 0

Views: 10459

Answers (2)

Himanshu
Himanshu

Reputation: 12685

The error is because you are trying to debug non main package. Debug the file from main package if you have settings to debug the file with main function. Else you can create setting for debugging the whole package. But for your problem just change the package name as

package main

import "fmt"

// SendEmail - Sends The email
func SendEmail() {

}

func main() {
    fmt.Println("Hello World!")
    i := 101
    fmt.Println(i)
}

or Run debugger from the main package with main function. To debug the whole workspace change the settings in launch.json as:

{
    "name": "Remote",
    "type": "go",
    "request": "launch",
    "mode": "remote",
    "remotePath": "${workspaceRoot}",
    "port": 2345,
    "host": "127.0.0.1",
    "program": "${workspaceRoot}",
    "env": {}
}

Upvotes: 2

Adrian
Adrian

Reputation: 46602

As the error implies, you can only debug a main package (a package named main) - specifically, because you can only run a main package. In Go, a binary's starting point is the function called main() in the package called main. In your code you have a function called main(), but it's in a package called email, which cannot be built into a program, and therefor cannot be debugged.

This is covered on the first page of the Go tour: https://tour.golang.org/basics/1

Programs start running in package main.

As well as in the spec: https://golang.org/ref/spec#Program_execution

A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value.

Upvotes: 2

Related Questions