当前位置: 首页 > 科技观察

写Go项目的两个烦人的事情?本文讨论其中的一个

时间:2023-03-20 19:46:34 科技观察

写Golang项目有两件事很烦人:一是错误处理中不断的“iferr!=nil”,二是作为编译型语言,代码不能你可以实时看到效果。借助一些工具,可以实现文件修改后自动编译重启,如:Linux环境:inotify-tools[1](GolangAutomaticReloads[2])Mac环境:fswatch[3]但是常用工具要么做不是span平台要么操作复杂。幸运的是,我找到了一个facebook出品的神器:watchman[4],不仅跨平台,而且操作简单。你只需要写一个脚本来连接它。当项目代码发生变化时,它会自动调用你的脚本。watchmanwatch[5]/path/to/your/projectwatchman—trigger[6]/path/to/your/project-p'\.go$'—/path/to/your/script注意:如果遇到问题,你可以查看Log"/usr/local/var/run/watchman/*-state/log"注意:如果你的编辑器有自动保存等功能,一定要记得关掉,比如vscode:除了watch,automaticsaving也是还有一些其他的选项,比如air[7]也不错,这里有一篇介绍文章:Go开发过程中Alwaysstopandcompile:用它来解决你的烦恼,快乐编程。参考资料[1].inotify-tools:https://github.com/inotify-tools/inotify-tools[2].GolangAutomaticReloads: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/空气