0

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
mornindew
  • 1,993
  • 6
  • 32
  • 54

2 Answers2

2

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.

Adrian
  • 42,911
  • 6
  • 107
  • 99
2

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": {}
}
Himanshu
  • 12,071
  • 7
  • 46
  • 61
  • Thank you very much, you were right. You made me think that I have misconfigured my repo. I added a question to the original post on how I setup my repo and maybe wonder if there is a better way to do it. – mornindew Aug 30 '18 at 16:42
  • @mornindew yes it is structured correctly. just always add a package for a different part of the functionality for example you want to create a functionality to send an email or suppose you want to create a package for handling the database queries. – Himanshu Aug 30 '18 at 16:42
  • Thanks. "program": "${workspaceRoot}" worked for me. Earlier it was "program": "${fileDirname}" – Prateek Bhuwania Jun 19 '19 at 17:24