preface

Hello everyone, I am a PHPer and still masturbating in ThinkPHP 3.2. As we all know, PHP is a dynamic language, the development environment responds in real time, and then in Go this static language, it is very awkward!

As a beginner, I have been torturing the Go hot compilation for a long time. Besides baidu, Google (English is poor, relying on translation), and looking up information in wechat and other social group chat, THERE are no other leaders to guide me and relevant complete solutions, which makes me take many detours!!

Okay, so without further ado, let’s get right to the subject and show you how to implement Go hot compilation + live Debug

The project structure

. ├ ─ ─. Air. Conf ├ ─ ─ a makefile ├ ─ ─ CMD │ └ ─ ─ serve │ └ ─ ─ main. Go # there is Gin logic └ ─ ─ scripts └ ─ ─ run. ShCopy the code

Yes, that’s right, it’s that simple!

Environment configuration

  • Air installationhttps://github.com/cosmtrek/air
  • DLV installationhttps://github.com/go-delve/delve

PS: If not, please go to the airport

scripts/run.shfile

# Kill the air process to prevent multiple launches, kill the air process that was previously running
ps -ef | grep -w air | grep -v grep | sort -k 2rn | awk '{if (NR>1){print $2}}' | xargs kill9 -Kill the serve process. This port is the port on which gin is running
lsof -i:8080 | grep serve | awk '{print $2}' | xargs kill9 -Kill the DLV process on the port where the DLV is running
lsof -i:2345 | grep dlv | awk '{print $2}' | xargs kill9 -# Debug Gin project
dlv debug --listen=:2345 --headless=true --api-version=2 --continue --accept-multiclient --output=./tmp/serve ./cmd/serve/main.go
Copy the code

makefilefile

run-dev:
	ENV=dev ./scripts/run.sh
Copy the code

air.conffile

  • The main routinefull_binConfigure, here we go Makefile
# 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 = "echo start build"

# Binary file yields from `cmd`.
# bin = "./tmp/main"

# Customize binary.
full_bin = "make run-dev"

# Watch these filename extensions.
include_ext = ["go", "yaml", "conf", "ttf"]

# Ignore these filename extensions or directories.
exclude_dir = ["tmp", "http"]

# Watch these directories if you specified.
include_dir = []

# Exclude files.
exclude_file = []

# Exclude unchanged files.
exclude_unchanged = true

# 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 = 200 # ms

[log]
# Show log time
time = true

[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

Copy the code

Finally, king fry

In the project directory terminal to execute: AIR, you can successfully hot compilation and real-time debug

Attach the project address

Github.com/smithyj/yan…