写Go项目有两件很烦的事情?本文讨论其中之一

开发 前端
写 Golang 项目有两件很烦的事情:一件是错误处理时连绵不绝的「if err != nil」,另一件是作为编译型语言,代码修改后不能实时看到效果。

写 Golang 项目有两件很烦的事情:一件是错误处理时连绵不绝的「if err != nil」,另一件是作为编译型语言,代码修改后不能实时看到效果,

借助一些工具可以实现文件修改后自动编译重启,比如:

  • Linux 环境:inotify-tools[1](Golang Automatic Reloads[2])
  • Mac 环境:fswatch[3]

不过常见的工具要么不跨平台,要么操作复杂,好在我发现了一个 facebook 出品的神器:watchman[4],不仅跨平台,而且操作简单,你只要写个脚本对接上就行了,当项目代码改变时,它会自动调用你的脚本。

 

  1. watchman watch[5] /path/to/your/project  
  2. watchman — trigger[6] /path/to/your/project -p '\.go$' — /path/to/your/script 

注意:如果遇到问题可以查日志「/usr/local/var/run/watchman/*-state/log」

注意:如果你的编辑器有自动保存之类的功能,务必记得关闭它,比如 vscode:

 

写 Go 项目有两件很烦的事情?本文讨论其中之一

自动保存

除了 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

 

责任编辑:未丽燕 来源: 今日头条
相关推荐

2022-05-31 17:23:51

加密货币数据机器人

2024-03-25 11:23:08

2021-10-18 05:42:23

代码编码开发

2020-06-09 14:30:17

编程命名代码

2013-12-12 10:55:21

2009-12-28 09:09:34

Google开源项目

2019-08-14 10:36:23

云计算云运维安全

2020-10-09 10:51:42

部署

2020-09-01 08:06:54

Kubernetes资源

2012-03-23 14:38:31

JavaScript

2012-04-10 10:01:10

Ubuntu安装事项

2020-10-25 08:47:36

Python有序字典

2014-10-15 10:49:27

程序员

2016-09-18 16:13:30

2023-03-19 16:08:44

2009-03-12 10:48:30

2018-05-07 15:32:54

编程语言Python程序员

2019-01-30 08:39:14

人脸识别谷歌AI

2011-12-29 09:15:19

开发
点赞
收藏

51CTO技术栈公众号