I wrote my first golang program. May I ask for a code-review please?

blov · · 442 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I started in Python about a year ago but have really wanted to try my hand at some go so I thought I&#39;d give it a shot. </p> <p>I wrote a script that monitors a list of files last modified time and runs a given shell command if any of them are updated. It runs in a loop so It has to kill the previous process if it&#39;s still running and one of my concerns is if I handled that correctly. </p> <p>Any sort of feedback would be amazing. And don&#39;t hesitate to give it to me straight either!</p> <p>That said, I&#39;ve had a blast with go so far and look forward to learning more about it from some of the community.</p> <p>Cheers.</p> <p>Github: <a href="https://github.com/Duroktar/trabWatcher">https://github.com/Duroktar/trabWatcher</a></p> <p>Followup: I put everyone&#39;s suggestions in the readme under a TODO section and I&#39;ll be checking them off as I go through. Thanks for the advice and if you manage to follow up on my changes thanks for that as well. Cheers</p> <hr/>**评论:**<br/><br/>pinpinbo: <pre><p>The code looks alright, but...</p> <p>Why not use inotify? instead of ticking every X seconds, you can actually get near real time update.</p></pre>Duroktar: <pre><p>Okay! Thanks for the tip, I&#39;m checking it out now +1</p> <p>edit: seems it&#39;s now <a href="https://fsnotify.org/" rel="nofollow">fsnotify</a> in case anyone else is looking</p> <p>followup: Hey! I was actually looking forward to having something similar like this brought to my attention. I wasn&#39;t so much concerned about the originality, I really just wanted to get my hands dirty. And in the end it&#39;s nice to have something I can directly reference for ideas/guidance (like using select and defer, which I totally overlooked). Thanks again :)</p></pre>nathankerr: <pre><p>Here are some similar tools:</p> <p><a href="https://golanglibs.com/category/auto-rebuild?sort=top" rel="nofollow">https://golanglibs.com/category/auto-rebuild?sort=top</a></p></pre>Morgahl: <pre><p>The best learning experience is getting your hands dirty on something. Even if you find out something else exists that solves the problem in a different/better way you have an understanding of the fundamentals of the problem and are better informed!</p></pre>Duroktar: <pre><p>This is so very true my friend. Come to think of it, I don&#39;t think I&#39;ve ever managed to code something that (upon further searching) doesn&#39;t already exist :D</p></pre>albn2: <pre><p>You could probably put watcher in a different package. You exported NewWatcher but then only use it from main, so it doesn&#39;t need to be exported. Same for Watcher struct and exported variables in Watcher.</p> <p>You can also invert some of your if statements to avoid indenting the whole function. The function at the end of the watcher file is a good candidate. Use an early return instead.</p></pre>Duroktar: <pre><p>Great stuff, I&#39;m still wrapping my head around the packaging system so that first bit is quite helpful. Good tip for keeping indents reasonable also, I&#39;ll be refactoring those bits tonight when I&#39;m free. Cheers +1 </p></pre>komkahh: <pre><p>What about a stop method for the Watcher?</p></pre>JohnGB: <pre><p>This could be taken as a style issue as in this simple case isn&#39;t much of a problem. However, you have nothing but the bare minimum of commenting in your code (basically just what is required to pass govet).</p> <p>Think of commenting as two things: 1) The intended logic of your code. It&#39;s fairly easy to see what your code is doing, but without explicit intent it&#39;s not clear if what it is doing is what you intended on it doing. 2) Documentation for both yourself in the future and for anyone else that reads your code.</p></pre>Duroktar: <pre><p>You are right of course. I usually do a better job than what you saw here, and don&#39;t really have an excuse other than I was being lazy. I will make it a point to write better comments in the future. Thank you!</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

442 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传