写 Golang 项目有两件很烦的事情:一件是错误处置时连绵不绝的「if err != nil」,另一件是作为编译型言语,代码修正后不能实时看到效果,
借助一些工具可以完成文件修正后自动编译重启,比如:
Linux 环境:inotify-tools[1](Golang Automatic Reloads[2])
Mac 环境:fswatch[3]
不过常见的工具要么不跨平台,要么操作复杂,好在我发现了一个 facebook 出品的神器:watchman[4],不只跨平台,而且操作复杂,你只需写个脚本对接上就行了,当项目代码改动时,它会自动调用你的脚本。
watchman watch[5] /path/to/your/project
watchman — trigger[6] /path/to/your/project -p '\.go$' — /path/to/your/script
留意:假设遇到成绩可以查日志「/usr/local/var/run/watchman/*-state/log」
留意:假设你的编辑器有自动保存之类的功用,务必记得封锁它,比如 vscode:
自动保存
除了 watch 之外,还有一些别的选择,比如 air[7] 也不错,这里有一篇引见文章:Go开发进程中总是要停上去编译:用它处置你的懊恼,快乐编程。
参考材料
[1].inotify-tools: https://github.com/inotify-tools/inotify-tools
[2].Golang Automatic Reloads: https://www.alexedwards.net/blog/golang-automatic-reloads
[3].fswatch: https://github.com/emcrisostomo/fswatch
[4].watchman: https://facebook.github.io/watchman/
[5].watch: https://facebook.github.io/watchman/docs/cmd/watch.html
[6].trigger: https://facebook.github.io/watchman/docs/cmd/trigger.html
[7].air: https://github.com/cosmtrek/air
【编辑引荐】
Golang还是Python?哪种言语更适宜AI?
初学Golang言语需求留意那些坑
谷歌的言语何以打败Python?50%的人都应该立刻学习Golang
Golang:三个常见的编程成绩
云原生的 Java与Golang
(责任编辑:admin)