Skip to main content

Go语言基础之Gin框架的热启动

Air的安装

  1. go

    go get -u github.com/cosmtrek/air
  2. MacOS

    curl -fLo air https://git.io/darwin_air
  3. Linux

    curl -fLo air https://git.io/linux_air
  4. Windows

    curl -fLo air.exe https://git.io/windows_air
  5. Docker

    docker run -it --rm \
    -w "<PROJECT>" \
    -e "air_wd=<PROJECT>" \
    -v $(pwd):<PROJECT> \
    -p <PORT>:<APP SERVER PORT> \
    cosmtrek/air
    -c <CONF>


    # 然后按照下面的方式在docker中运行你的项目
    docker run -it --rm \
    -w "/go/src/github.com/cosmtrek/hub" \
    -v $(pwd):/go/src/github.com/cosmtrek/hub \
    -p 9090:9090 \
    cosmtrek/air

咋用啊?

  1. 为了一会儿爽,先把alias air='~/.air'这句添加到你的(也是我的).bashrc或者.zshrc中,别问,问就是为了一会儿爽!

  2. 进入你的项目目录

    cd /path/to/your_project
  3. 在当前目录创建一个新的配置文件.air.conf

  4. 将下面这些内容复制到.air.conf配置文件中,可以根据你的喜好去改

    # [Air](https://github.com/cosmtrek/air) TOML 格式的配置文件

    # 工作目录
    # 使用 . 或绝对路径,请注意 `tmp_dir` 目录必须在 `root` 目录下
    root = "."
    tmp_dir = "tmp"

    [build]
    # 只需要写你平常编译使用的shell命令。你也可以使用 `make`
    # Windows平台示例: cmd = "go build -o ./tmp/main.exe ."
    cmd = "go build -o ./tmp/main ."
    # 由`cmd`命令得到的二进制文件名
    # Windows平台示例:bin = "tmp/main.exe"
    bin = "tmp/main"
    # 自定义执行程序的命令,可以添加额外的编译标识例如添加 GIN_MODE=release
    # Windows平台示例:full_bin = "./tmp/main.exe"
    full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
    # 监听以下文件扩展名的文件.
    include_ext = ["go", "tpl", "tmpl", "html"]
    # 忽略这些文件扩展名或目录
    exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
    # 监听以下指定目录的文件
    include_dir = []
    # 排除以下文件
    exclude_file = []
    # 如果文件更改过于频繁,则没有必要在每次更改时都触发构建。可以设置触发构建的延迟时间
    delay = 1000 # ms
    # 发生构建错误时,停止运行旧的二进制文件。
    stop_on_error = true
    # air的日志文件名,该日志文件放置在你的`tmp_dir`中
    log = "air_errors.log"

    [log]
    # 显示日志时间
    time = true

    [color]
    # 自定义每个部分显示的颜色。如果找不到颜色,使用原始的应用程序日志。
    main = "magenta"
    watcher = "cyan"
    build = "yellow"
    runner = "green"

    [misc]
    # 退出时删除tmp目录
    clean_on_exit = true
  5. 直接在项目目录下,运行air爽到不能呼吸!

附带一个 window 的配置

# 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.exe main.go"
# Binary file yields from `cmd`.
bin = "tmp/main.exe"
# Customize binary.
# 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 = []
# Exclude specific regular expressions.
exclude_regex = []
# Exclude unchanged files.
exclude_unchanged = true
# Follow symlink for directories
follow_symlink = 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 = 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