哪些场景可以用到远程调试?

  • Apple M1 M2 M3 不支持go 1.14 之前的版本
  • Golang 部分依赖不支持 window

这两个场景推荐使用 Docker。

Debugging a Go application inside a Docker container | The GoLand Blog
Updated and validated on November 17, 2022. You can find more tutorials on how to use containers here. You may also refer to the Docker and Kubernetes sections of our Help documentation. Go develop

以下是一个 Dockerfile 示例:


# 使用 1.14.15 镜像
FROM golang:1.14.15 as builder

# 下载 delve-1.6.1 源码到 当前项目,记得加到项目的 .gitignore
ADD delve-1.6.1 /go/delve
WORKDIR /go/delve/delve-1.6.1
# 开启代理编译安装 dlv
RUN https_proxy=http://192.168.31.2:1087 http_proxy=http://192.168.31.2:1087 \
 go install github.com/go-delve/delve/cmd/dlv

# 拷贝代码
COPY . /build
WORKDIR /build/web
# 开始编译
RUN https_proxy=http://192.168.31.2:1087 http_proxy=http://192.168.31.2:1087 \
	GO111MODULE=on GOPROXY="https://goproxy.cn,direct" \
	GOPRIVATE="codeup.aliyun.com" \
  go build -o bin/web \
    # 必须加上下面一行编译参数
		-gcflags "all=-N -l" \
    -tags=jsoniter,prototype main.go


FROM debian:buster

EXPOSE 8080 40001

WORKDIR /
COPY --from=builder /build/web /web
RUN ls /
COPY --from=builder /go/bin/dlv /web/
RUN ls /web
COPY --from=builder /build/web/bin/web /web/bin/web

WORKDIR /web

#ENTRYPOINT [ "/bookadv-cmd" ]
CMD ["/web/dlv", \
"--listen=:40001", "--headless=true", "--api-version=2", "--accept-multiclient", \
## 需要注意,如果使用项目自身的参数,需要加上 "--",否则会认为是 dlv 的参数进而发生错误。
"exec", "/web/bin/web", "--", "--server_address=:8080", "http"]