doushang7209 2018-08-30 15:46
浏览 143
已采纳

Golang调试器未运行

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?

  • 写回答

2条回答 默认 最新

  • donglu8549 2018-08-30 15:49
    关注

    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": {}
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?