55

用 air 给 Golang/Gin 框架提供热重启支持

 6 years ago
source link: https://studygolang.com/articles/18290?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Golang 这种编译语言和 Rails/Python 这种脚本语言不同, 脚本语言做后端, 只要不是配置和数据库有变化, 一般都是源代码一修改后, 最新后端服务就立即生效了.

但是 Golang 做后端, 需要每次修改代码以后都要重新编译后重启服务才行.

但是每次改完代码都切换到终端按 Ctrl + C 也是无比的烦.

对于我这种懒得写代码的懒人, 伟大的Google从来都有现成的方案, 在Mac上直接用下面的命令在后端目录下执行:

$ curl -fLo ~/air \
    https://raw.githubusercontent.com/cosmtrek/air/master/bin/darwin/air

$ chmod +x ~/air

$ ./air

就是这么简单, 配合 FRP, 直接在本地开发, 前端直接调用就可以通过阿里云反向代理到本地开发机进行访问了.

只要 Golang 源代码一改, air 会自动重新编译并重启, 超级方便, 再也不用按 Ctrl + C 了.

注意: air 这种重新编译和重启的方案只适合开发环境, 如果生产环境, 还需要基于 Golang HTTP.shutdown 方案开发更加智能的子进程热替换方案, 否则会因为重启瞬间丢失用户访问请求.

That's all, gopher! ;)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK