前言
哈喽,大家好,我是asong,这是我的第九篇原创文章。今天给大介绍几种热加载的方法,大大提高我们的开发效率。
什么是热加载如果你是一名python开发者,应该很熟悉这个。我们在Flask或者Django框架下开发都是支持实时加载的,当我们对代码进行修改时,程序能够自动重新加载并执行,这在我们开发中是非常便利的,可以快速进行代码测试,省去了每次手动重新编译。
如果你是一名JAVA开发者,不仅会听过热加载,热部署会跟着一块出现。热部署一般是指容器(支持多应用)不重启,单独启动单个应用。热加载一般指重启应用(JVM),单独重新更新某个类或者配置文件。
知道了什么是热加载了,想在项目开发中使用,该怎么做呢?下面就来介绍几种方法,想用哪个就用哪个,就是这么豪横。哼!!!
1. Air
github地址:https://github.com/cosmtrek/air Star:1.8k
它具有以下特性:彩色日志输出
自定义构建或二进制命令
支持忽略子目录
启动后支持监听新目录
更好的构建过程
安装
1$ go get -u github.com/cosmtrek/air
使用
为了我们能够好的使用命令行操作,我们需要把alias air='~/.air'加到你的.bashrc或.zshrc中,根据你的系统进行选择,因为我是mac,所以我将alias air='~/.air'加到了vim ~/.zshrc中了。
然后命令下执行如下操作
1# 1. 进入你自己的项目目录
2$ cd /your_project
3# 2. 查找你的项目中是否存在 `.air.conf` 配置文件
4$ air -c .air.conf
5# 3. 没有则创建一个
6$ touch .air.conf
7# 4. 复制下面示例 `air.conf.example`到你的 `.air.conf`
8
9# 5. 启动热加载
10$ air
11# 6. 启动热加载 带打印log
12$ air -d
air.conf.example示例
参考:传送门
1root = "."
2tmp_dir = "tmp"
3
4[build]
5# Just plain old shell command. You could use `make` as well.
6cmd = "go build -o ./tmp/main ."
7# Binary file yields from `cmd`.
8bin = "tmp/main"
9# Customize binary.
10full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
11# Watch these filename extensions.
12include_ext = ["go", "tpl", "tmpl", "html"]
13# Ignore these filename exte