我认为这种情况下可能存在问题。这是我所做的:
c:\repos\demo
go mod init demo
,这是mod文件
module demo
go 1.22.0
main.go
文件
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
})
http.ListenAndServe(":8080", nil)
}
http://localhost:8080
来命中断点。FROM golang:1.22.0
WORKDIR /app
COPY . .
RUN go install github.com/go-delve/delve/cmd/dlv@latest
EXPOSE 40000 8080
CMD ["dlv", "debug", "--headless", "--listen=:40000", "--api-version=2", "--accept-multiclient", "--log"]
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Connect to server",
"type": "go",
"request": "attach",
"mode": "remote",
"port": 40000,
"host": "127.0.0.1"
}
]
}
docker build -t demo .
docker run -p 40000:40000 -p 8080:8080 demo
Connect to server
进行远程调试
PS C:\repos\demo> docker run -p 40000:40000 -p 8080:8080 demo
2024-02-16T04:46:42Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
2024-02-16T04:46:42Z info layer=debugger launching process with args: [./__debug_bin4263375465]
API server listening at: [::]:40000
2024-02-16T04:46:42Z debug layer=debugger Adding target 1534 "/app/__debug_bin4263375465"
2024-02-16T04:46:50Z debug layer=debugger continuing
2024-02-16T04:46:50Z debug layer=debugger ContinueOnce
2024-02-16T04:46:59Z debug layer=debugger halting
2024-02-16T04:46:59Z warning layer=debugger gnu_debuglink link "ce4e6e4ef08fa58a3535f7437bd3e592db5ac0.debug" not found in any debug info directory
2024-02-16T04:46:59Z warning layer=debugger gnu_debuglink link "e7d4a67acf053c794b3b8094e6900b5163f37d.debug" not found in any debug info directory
2024-02-16T04:46:59Z debug layer=debugger callInjection protocol on:
2024-02-16T04:46:59Z debug layer=debugger 1534 PC=0x405e8e
2024-02-16T04:46:59Z debug layer=debugger 1542 PC=0x4795a3
2024-02-16T04:46:59Z debug layer=debugger 1543 PC=0x4795a3
2024-02-16T04:46:59Z debug layer=debugger 1544 PC=0x4795a3
2024-02-16T04:46:59Z debug layer=debugger 1545 PC=0x4795a3
2024-02-16T04:46:59Z debug layer=debugger continuing (direction congruent)
2024-02-16T04:46:59Z debug layer=debugger ContinueOnce
大约一个月前,我尝试运行相同的代码,一切都运行良好。不过,这一次似乎坏了。我不确定最近是否有什么变化。
这对我有用 - 打开 VSCode 用户的 settings.json 文件并在其中添加:
"go.delveConfig": {
"debugAdapter": "legacy",
},