使用Go和GoLand IDE进行远程调试HTTP请求

I am new to go, delve and GoLand IDE. I would like to remote debug some REST endpoints that are deployed using make and run in a docker (using docker + docker-compose).

The command I use to bring up my environment is make myproject

My endpoints get hosted at: localhost:8080

When creating a debug configuration in GoLand it specifies that before running dlv debug --headless --listen=:2345 --api-version 2, do the following:

go build -gcflags='-N -l' github.com/myproject
dlv --listen=:2345 --headless=true --api version=2 exec ./myproject

Is there a way for me to attach to my project once it's running on localhost:8080? How will these commands differ if that is the case?

Thanks in advance

This command dlv debug --headless --listen=:2345 --api-version 2 your/package/name allows delve to compile the package then launch itself and the compiled binary.

On the other hand, these commands go build -gcflags='-N -l' github.com/myproject dlv --listen=:2345 --headless=true --api version=2 exec ./myproject show you that you can compile the binary separately, in case you need more flexibility and then launch the debugger. Keep in mind that it's really important to add -gcflags='-N -l' or -gcflags='all=-N -l', depending on which Go version you have, in order for the debugger to be able to have more data available about your application.

As for the:

Is there a way for me to attach to my project once it's running on localhost:8080? How will these commands differ if that is the case?

It depends on where you are running the process. If it's on your machine directly, without a VM or a container, then the IDE has the option in Run|Attach to Process. If the process is running on a different machine then you need to login into that machine and use dlv --headless --listen=:2345 --api-version 2 attach <pid>.

We published an article dedicated to this problem a couple of months ago, please have a look at it for a more detailed reply.