zoukankan      html  css  js  c++  java
  • 使用 Air 热编译 Gin 项目

    Air 是一款能够自动监控代码并进行热编译 Golang 项目的工具,并不局限于 Gin 项目。

    下载

    官方文档推荐了几种下载方式:

    Go

    go get -u github.com/cosmtrek/air
    

    macOS

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

    Linux

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

    Windows

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

    但是笔者在使用go get时会失败,哪怕是项目使用了go mod之后。使用 github 下载由于众所周知的原因异常慢,其实有用的就是一个编译后的二进制文件,笔者已经下好了,点这里

    蓝奏云只能上传特定类型的文件,其中air.e为 linux 版,下载后请重命名为air

    将下载好的air.exe文件(linux下为air文件)移动到GOBIN路径下(GOBIN环境变量已加入到环境变量PATH中),以便在任意路径都可以运行air命令。

    使用

    使用默认的配置文件

    直接在 Gin 项目的根路径下运行以下命令即可,后续修改代码都会自动进行构建运行:

    air
    

    首先会先在当前目录下找.air.conf文件,找不到就使用 air 默认的配置。

    指定配置文件

    在项目根路径下新建配置文件.air.conf,使用以下命令:

    air -c .air.conf
    

    由于配置文件名是.air.conf,故可以省略,直接运行命令air即可。

    配置文件如下:

    Windows版

    # [Air](https://github.com/cosmtrek/air) TOML 格式的配置文件
    
    # 工作目录
    # 使用 . 或绝对路径,请注意 `tmp_dir` 目录必须在 `root` 目录下
    root = "."
    tmp_dir = "tmp"
    
    [build]
    # 只需要写你平常编译使用的shell命令。你也可以使用 `make`
    cmd = "go build -o ./tmp/main.exe ."
    # 由`cmd`命令得到的二进制文件名
    bin = "tmp/main.exe"
    # 自定义的二进制,可以添加额外的编译标识例如添加 GIN_MODE=release
    full_bin = "SET APP_ENV=dev & SET APP_USER=air & .	mpmain.exe"
    # 监听以下文件扩展名的文件.
    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
    

    Linux版

    # [Air](https://github.com/cosmtrek/air) TOML 格式的配置文件
    
    # 工作目录
    # 使用 . 或绝对路径,请注意 `tmp_dir` 目录必须在 `root` 目录下
    root = "."
    tmp_dir = "tmp"
    
    [build]
    # 只需要写你平常编译使用的shell命令。你也可以使用 `make`
    cmd = "go build -o ./tmp/main ."
    # 由`cmd`命令得到的二进制文件名
    bin = "tmp/main"
    # 自定义的二进制,可以添加额外的编译标识例如添加 GIN_MODE=release
    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
    

    2 个版本最主要的差别就是 cmd、bin、full_bin 中的内容,Windows 版生成的目标文件是带.exe后缀的,Linux 版不带;full_bin 中的内容,Windows 版设置环境变量并运行是通过SET APP_ENV=dev & SET APP_USER=air & . mpmain.exe来进行的,而 Linux 版是通过APP_ENV=dev APP_USER=air ./tmp/main来进行的。

  • 相关阅读:
    P3919 【模板】可持久化线段树 1(可持久化数组)
    P3384 【模板】轻重链剖分
    P2680_运输计划
    CSP-S2 T4/P7078 贪吃蛇_set 70pts/100pts(O2)
    SPFA判负环
    P6394 樱花,还有你
    CSP-S2T4/P7078 贪吃蛇
    【模板】单源最短路径(标准版)
    U135649 皇室战争
    【离散数学】实验三 偏序关系中盖住关系的求取及格论中有补格的判定
  • 原文地址:https://www.cnblogs.com/loveshes/p/12890115.html
Copyright © 2011-2022 走看看