我喜歡能夠做非常簡(jiǎn)單的事情,等待客戶端連接到 dlv 調(diào)試器,然后再繼續(xù)代碼但沒有成功。我有簡(jiǎn)單的 go 服務(wù)器:package mainimport ( "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)}我像這樣在 linux 機(jī)器上啟動(dòng)服務(wù)器: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 --logAPI server listening at: [::]:30002022-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在 visual studio 代碼中,launch.json 如下所示:"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, } ]它可以很好地連接到遠(yuǎn)程服務(wù)器問題是我喜歡 dlv 調(diào)試服務(wù)器在執(zhí)行 go 代碼之前等待客戶端連接。我希望能夠在 func main() 第二行中設(shè)置斷點(diǎn)我怎樣才能做到這一點(diǎn) ?
1 回答

楊__羊羊
TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
你的第二行main.go
只是注冊(cè)你的處理函數(shù)(hello
在本例中)。這發(fā)生在您的應(yīng)用程序啟動(dòng)之前ListenAndServe()
。
我假設(shè)您想在啟動(dòng)后調(diào)試您的應(yīng)用程序。您可能希望在函數(shù)的第一行(也是唯一一行)設(shè)置斷點(diǎn)hello
。
- 1 回答
- 0 關(guān)注
- 163 瀏覽
添加回答
舉報(bào)
0/150
提交
取消