• Air实现Go程序的热重载(热加载)


    简介:

    air是Go的热加载工具,它可以监听文件或者目录的变化,自动编译,重启程序,提高开发的工作效率。

    场景:

    在代码修改后需要通过ctrl+c来停止项目,go run的方式来再次重启项目,在开发进行中频繁操作会很麻烦,很影响开发的效率,air刚好解决了这种问题

    Air的特性:

    1. 彩色日志输出
    2. 自定义构建或二进制命令
    3. 支持忽略子目录
    4. 启动后支持监听新目录
    5. 更好的构建过程

    安装方式:

    1、Go最原始的安装方法,但是配置文件会容易出问题:

    go get -u github.com/cosmtrek/air
    
    • 1

    2、使用go install安装方法,注:使用 Go 的版本为 1.16 或更高
    需要配置.bashrc.zshrcalias air=‘$(go env GOPATH)/bin/air’

    go install github.com/cosmtrek/air@latest
    
    • 1

    3、推荐使用 install.sh,但是博主本人使用的是第二种方式

    # binary 文件会是在 $(go env GOPATH)/bin/air
    curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
    
    # 或者把它安装在 ./bin/ 路径下
    curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s
    
    air -v
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4、Docker的安装方式

    docker run -it --rm \
        -w "" \
        -e "air_wd=" \
        -v $(pwd):<PROJECT> \
        -p <PORT>:<APP SERVER PORT> \
        cosmtrek/air
        -c <CONF>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    然后按照下面的方式在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
    • 2
    • 3
    • 4
    • 5

    使用方式(同官网的使用方式):

    首先,进入你的项目文件夹

    cd /path/to/your_project
    
    • 1

    最简单的方法是执行

    # 优先在当前路径查找 `.air.toml` 后缀的文件,如果没有找到,则使用默认的
    air -c .air.toml
    
    • 1
    • 2

    您可以运行以下命令初始化,把默认配置添加到当前路径下的.air.toml 文件。

    air init
    
    • 1

    在这之后,你只需执行 air 命令,无需添加额外的变量,它就能使用 .air.toml 文件中的配置了。

    air
    
    • 1

    如欲修改配置信息,请参考 air_example.toml 文件.

    运行时参数

    # 会执行 ./tmp/main bench
    air bench
    
    # 会执行 ./tmp/main server --port 8080
    air server --port 8080
    
    • 1
    • 2
    • 3
    • 4
    • 5

    可以使用 – 参数分隔为 air 命令传递的参数和构建的二进制文件。

    # 会运行 ./tmp/main -h
    air -- -h
    
    # 会使用个性化配置来运行 air,然后把 -h 后的变量和值添加到运行的参数中
    air -c .air.toml -- -h
    
    • 1
    • 2
    • 3
    • 4
    • 5

    air_example.toml示例

    完整的官网配置信息,根据需要修改。

    # [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 = []
    # 忽略符合通过正则匹配到的文件
    exclude_regex = ["_test\\.go"]
    # 忽略未进行修改的文件
    exclude_unchanged = true
    # 按照目录的符号链接
    follow_symlink = true
    # 这个日志文件放在你的`tmp_dir`中
    log = "air.log"
    # 如果文件更改过于频繁,则没有必要在每次更改时都触发构建。可以设置触发构建的延迟时间/毫秒
    delay = 1000 # ms
    # 发生构建错误时,停止运行旧的二进制文件
    stop_on_error = true
    # 杀死进程前发送中断信号(Windows不支持)
    send_interrupt = false
    # 发送中断信号后延迟时间/毫秒
    kill_delay = 500 # ms
    # 在运行二进制文件时添加额外的参数 (bin/full_bin)。将运行“./tmp/main hello world”
    args_bin = ["hello", "world"]
    
    [log]
    # 显示日志时间
    time = false
    
    [color]
    # 自定义每个部分的颜色。如果未找到颜色,请使用原始应用程序日志。
    main = "magenta"
    watcher = "cyan"
    build = "yellow"
    runner = "green"
    
    [misc]
    # 退出时删除 tmp 目录
    clean_on_exit = true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    至此按照使用方法先执行:air init
    然后执行:air
    热加载就启动了

    调试

    运行 air -d 命令能打印所有日志。

    遇到 “command not found: air” 或 “No such file or directory”

    export GOPATH=$HOME/xxxxx
    export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
    export PATH=$PATH:$(go env GOPATH)/bin <---- 请确认这行在您的配置信息中!!!
    
    • 1
    • 2
    • 3

    部署

    请注意:这需要 Go 1.16+ ,因为使用 go mod 来管理依赖。

    # 1. 先fork项目
    
    # 2. 然后克隆clone下来
    mkdir -p $GOPATH/src/github.com/cosmtrek
    cd $GOPATH/src/github.com/cosmtrek
    git clone git@github.com:<YOUR USERNAME>/air.git
    
    # 3. 切换路径安装依赖
    cd air
    make ci
    
    # 4. 这样就可以使用了!
    make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    im即时通讯开发之Android进程保活详解
    数据库容灾 | MySQL MGR与阿里云PolarDB-X Paxos的深度对比
    NNDL 实验七 循环神经网络(3)LSTM的记忆能力实验
    【Python百日进阶-WEB开发】Day179 - Django案例:11短信验证码
    关于useEvent的思考
    mac制作ssl证书|生成自签名证书,nodejs+express在mac上搭建https+wss(websocket)服务器
    前端八股文85-141
    【Linux基础】权限管理
    基于复杂网络的生产系统瓶颈簇识别方法
    基于最大熵图像插值Maximum Entropy插值算法的图像超分辨重构研究-附Matlab代码
  • 原文地址:https://blog.csdn.net/sinat_41672927/article/details/126137839