>本指南演示了設置docker容器中的GO應用程序的實時重載和調試,以鏡像Node.js開發工作流程。 雖然此特定GO設置的在線資源很少,但這種方法將一致的Docker環境的好處與實時重載的效率和調試的力量相結合。
>密鑰差異: live-realoading在代碼更改上重新啟動應用程序; >熱重載
修補了不重新啟動的內存。調試,對於有效開發至關重要,超過了基於日誌的故障排除。 Docker確保跨環境的應用程序行為一致。 >
>環境:本指南使用Windows 11帶有WSL(Windows子系統的Linux),但適用於Linux(相同的步驟)和MACOS(相似)。 強烈建議Windows Go開發WSL,因為它的速度與本機Windows File System相比。
技術堆棧:
Golang Server(光纖示例):
> 創建:
main.go
>用
<code class="language-go">package main import "github.com/gofiber/fiber/v2" func main() { app := fiber.New() app.Get("/", func(c *fiber.Ctx) error { str := "Hello, World!" return c.SendString(str) }) app.Listen(":3000") }</code>對此進行測試
go run .
> docker設置(
docker-compose.yml
<code class="language-yaml">api: build: context: ./api dockerfile: Dockerfile ports: - '3000:3000' - '2345:2345' stop_grace_period: 0.1s volumes: - ./api:/app networks: - internal</code>>
build.context
ports
目錄安裝到容器中。 volumes
api
> dockerfile(/app
):
api/Dockerfile
空氣和delve配置():
<code class="language-dockerfile">FROM golang:1.23.2-alpine3.20 WORKDIR /app RUN go install github.com/go-delve/delve/cmd/dlv@latest RUN go install github.com/air-verse/air@latest COPY go.mod go.sum ./ RUN go mod download USER root # For development only EXPOSE 2345 EXPOSE 3000 CMD ["air", "-c", "air.toml"]</code>
這將配置空氣使用Delve啟用了啟用調試的應用程序。
在
api/air.toml
此配置VS代碼以連接到Docker容器中運行的Delve調試器。 調整以匹配您的項目的路徑。
<code class="language-toml">root = "." tmp_dir = "tmp" [build] full_bin = "dlv debug --build-flags=\"-gcflags='all=-N -l'\" --listen 0.0.0.0:2345 --headless --continue --accept-multiclient --output=dist/debug"</code>這個完整的設置可以在Docker容器中進行實時重載和調試,以進行有效的GO開發。請記住在設置所有文件後運行
。
>以上是在 Docker 容器內即時重新載入和調試 Go 應用程式的詳細內容。更多資訊請關注PHP中文網其他相關文章!