I like to be able do very simple thing wait for the client to connect to dlv debugger before continue the code without success . i have simple go server :
package main
import (
"fmt"
"net/http"
)
func hello(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "hello\n")
}
func headers(w http.ResponseWriter, req *http.Request) {
for name, headers := range req.Header {
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
}
func main() {
fmt.Println("server started now!")
http.HandleFunc("/hello", hello)
http.HandleFunc("/headers", headers)
http.ListenAndServe(":8090", nil)
}
i start the server on the linux mechine like this:
vagrant@vagrant:~/go_dev/very_simple_server_dir$ dlv debug /home/vagrant/go_dev/very_simple_server_dir/very_simple_server.go --headless --listen=:3000 --log
API server listening at: [::]:3000
2022-10-31T06:18:47Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
2022-10-31T06:18:47Z info layer=debugger launching process with args: [/home/vagrant/go_dev/very_simple_server_dir/__debug_bin]
2022-10-31T06:18:47Z warning layer=debugger can't find build-id note on binary
in visual studio code the launch.json looks like this :
"version": "0.2.0",
"configurations": [
{
"name": "Connect to server",
"type": "go",
"request": "attach",
"mode": "remote",
"remotePath": "/home/vagrant/go_dev/very_simple_server_dir/",
"cwd" : "${workspaceFolder}",
"port": 3000,
"host": "127.0.0.1",
"trace": "verbose",
"asRoot": true,
}
]
it connectes just fine to the remote server the problem is that i like the dlv debug server wait to the client to connect before it execute the go code . i want to be able to set break point in func main() second line
how can i do that ?
The second line in your main.go
does no more than register your handler function (hello
in this case). This happens before your app is started with ListenAndServe()
.
I assume you want to debug your app after it is started. You may want to set a breakpoint at the first (and the only) line of your hello
function.