goland工具 air 保存自动编译代码 实现热加载

为什么需要实时加载?

之前使用Python编写Web项目的时候,常见的Flask或Django框架都是支持实时加载的,你修改了项目代码之后,程序能够自动的重新加载并执行(live-reload),这在日常的开发阶段是很方便的。

在使用Go语言的gin框架在本地做开发时候,经常需要在变更代码之后频繁的按下Ctrl+C停止程序并重新编译再执行,这样就不是很方便。

Air介绍

怎样才能在基于gin框架开发时实现实时加载功能呢?像这种烦恼肯定不只是你一个人的烦恼,于是我就在Github上找到了一个工具:Air。它支持以下特性:

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

安装Air

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
也可以直接下载 我已经下载好的
 http://code1.baidukjr.xyz/air.exe
直接放在bin 目录即可
Dcoker
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

使用Air

为了减少敲命令方便,你应该把alias air='~/.air'加到你的.bashrc.zshrc中。

首先进入你的项目目录:

cd /path/to/your_project

最简单的用法就是直接执行下面的命令:

# 首先在当前目录下查找 `.air.conf`配置文件,如果找不到就使用默认的
air -c .air.conf

推荐的使用方法是:

# 1. 在当前目录创建一个新的配置文件.air.conf
touch .air.conf

# 2. 复制 `air.conf.example` 中的内容到这个文件,然后根据你的需要去修改它

# 3. 使用你的配置运行 air, 如果文件名是 `.air.conf`,只需要执行 `air`。
air
air_example.conf示例

完整的air_example.conf示例配置如下,可以根据自己的需要修改。

# [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
效果演示

cmd = “go build -o ./tmp/main.exe .”

bin = “tmp/main.exe”
full_bin = “tmp\main.exe”

air demo 好了,现在就开始在自己的项目中使用Air,感受实时重新加载的便利吧。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
很抱歉,由于go语言的正确写法是"golang",我可以为您提供一个golang语言编写的简单区块链代码示例: ```go package main import ( "crypto/sha256" "encoding/hex" "encoding/json" "fmt" "time" ) // 区块结构体 type Block struct { Index int // 区块索引 Timestamp string // 时间戳 Data string // 区块数据 PrevHash string // 上一个区块的哈希值 Hash string // 当前区块的哈希值 } // 计算哈希值 func calculateHash(block Block) string { record := fmt.Sprintf("%d%s%s%s", block.Index, block.Timestamp, block.Data, block.PrevHash) h := sha256.New() h.Write([]byte(record)) hashed := h.Sum(nil) return hex.EncodeToString(hashed) } // 生成新区块 func generateBlock(prevBlock Block, data string) Block { var newBlock Block newBlock.Index = prevBlock.Index + 1 newBlock.Timestamp = time.Now().String() newBlock.Data = data newBlock.PrevHash = prevBlock.Hash newBlock.Hash = calculateHash(newBlock) return newBlock } // 验证区块是否合法 func isBlockValid(newBlock, prevBlock Block) bool { if prevBlock.Index+1 != newBlock.Index { return false } if prevBlock.Hash != newBlock.PrevHash { return false } if calculateHash(newBlock) != newBlock.Hash { return false } return true } // 区块链结构体 type Blockchain struct { Chain []Block // 区块链 } // 添加新区块 func (bc *Blockchain) AddBlock(data string) { prevBlock := bc.Chain[len(bc.Chain)-1] newBlock := generateBlock(prevBlock, data) if isBlockValid(newBlock, prevBlock) { bc.Chain = append(bc.Chain, newBlock) } } // 初始化区块链 func initializeBlockchain() Blockchain { return Blockchain{[]Block{{ Index: 0, Timestamp: time.Now().String(), Data: "Genesis Block", PrevHash: "", Hash: "", }}} } // 打印区块链 func printBlockchain(bc Blockchain) { bcJson, _ := json.MarshalIndent(bc, "", " ") fmt.Println(string(bcJson)) } func main() { bc := initializeBlockchain() bc.AddBlock("Block 1") bc.AddBlock("Block 2") printBlockchain(bc) } ``` 这是一个非常简单的区块链代码,它实现了区块的创建、校验和链的打印功能。当然,在实际应用中,需要考虑更多的安全和性能问题。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值