Building a development environment for the Go language framework Gin. (Hot reload, VS Code debugging support)
This code is published on GitHub. https://github.com/yolo-lin/go-demo
Gin: Go framework cosmtrek / air: Hot reload delve: Debugging
--Install Go: Install from Official --Turn on the environment variable GO111MODULE to manage dependencies in Go Modules
.zshrc
export GO111MODULE=on
--Initialization of Go Modules
/src
$go mod init package name
The directory structure of Gin is not fixed, so the following is my own style.
Place all the main source code under src. In the future, we plan to add models
and controllers
to src.
.
├── .vscode
│ └── launch.json
├── docker
│ ├── go
│ │ └── Dockerfile
│ └── mysql
│ └── Dockerfile
├── src
│ ├── go.mod
│ ├── go.sum
│ ├── .air.toml
│ └── main.go
│
└── docker-compose.yml
Docker Go
/docker/go/Dockerfile
FROM golang:1.15.2
ENV GO111MODULE=on
COPY src/ /go/src/app/
WORKDIR /go/src/app
# cosmtrek/installation of air
RUN go get -u github.com/cosmtrek/air
#installation of delve
RUN go get -u github.com/go-delve/delve/cmd/dlv
#Start air
CMD air -c .air.toml
MySQL
/docker/go/Dockerfile
FROM mysql:8.0
RUN chown -R mysql /var/lib/mysql && \
chgrp -R mysql /var/lib/mysql
docker-compose
yml:./docker-compose.yml
version: '3'
networks:
backend:
driver: bridge
services:
go:
container_name: go
build:
context: .
dockerfile: ./docker/go/Dockerfile
ports:
- 8080:8080
- 2345:2345
links:
- mysql
tty: true
volumes:
- ./src:/go/src/app
depends_on:
- mysql
security_opt:
- seccomp:unconfined
cap_add:
- SYS_PTRACE
networks:
- backend
mysql:
container_name: mysql
build:
context: .
dockerfile: ./docker/mysql/Dockerfile
environment:
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: demo
hostname: mysql
ports:
- "3306:3306"
volumes:
- ./docker/mysql/data:/var/lib/mysql
security_opt:
- seccomp:unconfined
networks:
- backend
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
- PMA_ARBITRARY=1
- PMA_HOST=mysql
- PMA_USER=root
- PMA_PASSWORD=password
ports:
- "8081:80"
depends_on:
- mysql
networks:
- backend
Divert the official air_example.toml
Added the description for debugging below
toml:/src/.air.toml
# Debug
#full_bin = "APP_ENV=dev APP_USER=air /go/bin/dlv exec ./tmp/main --headless=true --listen=:2345 --api-version=2 --accept-multiclient"
toml:/src/.air.toml
# Config file for [Air](https://github.com/cosmtrek/air) in TOML format
# Working directory
# . or absolute path, please note that the directories following must be under root.
root = "."
tmp_dir = "tmp"
[build]
# Just plain old shell command. You could use `make` as well.
cmd = "go build -o ./tmp/main ."
# Binary file yields from `cmd`.
bin = "tmp/main"
# Customize binary.
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
# Debug
#full_bin = "APP_ENV=dev APP_USER=air /go/bin/dlv exec ./tmp/main --headless=true --listen=:2345 --api-version=2 --accept-multiclient"
# Watch these filename extensions.
include_ext = ["go", "tpl", "tmpl", "html"]
# Ignore these filename extensions or directories.
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
# Watch these directories if you specified.
include_dir = []
# Exclude files.
exclude_file = []
# This log file places in your tmp_dir.
log = "air.log"
# It's not necessary to trigger build each time file changes if it's too frequent.
delay = 1000 # ms
# Stop running old binary when build errors occur.
stop_on_error = true
# Send Interrupt signal before killing process (windows does not support this feature)
send_interrupt = false
# Delay after sending Interrupt signal
kill_delay = 500 # ms
[log]
# Show log time
time = false
[color]
# Customize each part's color. If no color found, use the raw app log.
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"
[misc]
# Delete tmp directory on exit
clean_on_exit = true
Now, when docker-compose up -d
is done, the config file will be automatically adapted and hot reloaded.
vscode Install the extension of Go and add the debug configuration file below.
json:/.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Remote",
"type": "go",
"request": "launch",
"mode": "remote",
"program": "${workspaceFolder}",
"port": 2345,
"host": "127.0.0.1",
"showLog": true,
}
]
}
If you need to debug, change the cosmtrek / air settings.
After exchanging full_bin
for development and debugging and restarting docker-compose, you can debug with vscode.
toml:/src/.air.toml
# Customize binary.
# full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
# Debug
full_bin = "APP_ENV=dev APP_USER=air /go/bin/dlv exec ./tmp/main --headless=true --listen=:2345 --api-version=2 --accept-multiclient"
However, if you update the code, it will be automatically built from cosmtrek / air, but the vscode side will be disconnected and you will need to manually reconnect.
Recommended Posts