Best way to set Go macros?

blov · · 331 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello, fellow gophers.</p> <p>As I have been programming in golang, the main issue I have found with the language, other than sometimes subpar speed, is that a lot of the code is repetitive. For example, what is just a protected call or try block in other languages is in Go a block followed by checking the err and lots of branched conditions as a result that messes up my overall structure.</p> <p>Although I cannot fix or affect the first issue, I am doing my best to give the language a decent chance by having macros to input some of the various repeated snippets such as &#34;if err != nil { x }&#34;, leaving my cursor at x. Then I can have a configuration so only x is visible with some comment at the end.</p> <p>My main issue is that this feels kind of clunky. I am an experienced programmer in a multitude of other languages (JS,LUA,C++,Haskell) so in my efforts to be more productive I&#39;ve taken it as a reasonable precondition that it is not my style causing these issues.</p> <p>Based on this, I&#39;d like to ask other experienced devs on how they&#39;ve handled some of these shortcomings of the language. I get that it is new, but I really feel as though industry has moved a bit too hastily to it, personally indirectly causing my job to require me to learn it.</p> <p>We are developing a login server for our tool and it feels like I am just rewriting it in &#34;minimally expressionate&#34; C which constantly checks things that are pretty obviously a fixed value.</p> <p>So for those who actually use this language by choice, what&#39;s the trick? Or are most in the same boat as me?</p> <hr/>**评论:**<br/><br/>natefinch: <pre><p>It&#39;s not really that new at this point. 1.0 was 5.5 years ago. </p> <p>Most of us enjoy the explicitness of deciding exactly what to do with each error. Sure, at first ,and in trivial programs, you may just return every error as-is, but in bigger and more serious projects, you start to think about whether you log the error, add information to it, etc.</p> <p>I use it 40 hours a week at work and do all my side projects in it. What some people consider drawbacks, I find to be refreshingly simple and obvious. </p></pre>ZetaHunter: <pre><p>So much this, I&#39;ve never considered explicitness of the error handling to be a drawback.</p></pre>drvd: <pre><p>&#34;Get used to it.&#34; is the best advice I can give. So: get used to it.</p></pre>xbooing: <pre><p>Go is just a bad language then?</p></pre>natefinch: <pre><p>No. It&#39;s different. I find it a nice middle ground between the (IMO) overly explicit &amp; strictness of rust and the overly permissiveness of python. It fits in the same niche as java or C# without java&#39;s boilerplate, huge memory footprint, and tendency to over-abstract, and C#&#39;s neverending feature-itis.</p></pre>burnaftertweeting: <pre><p>Beauty is in the eye of the beholder. </p></pre>PaluMacil: <pre><p>I think you might be branching too much. If you return in your if block that detects an error, then you don&#39;t really have a lot of branching. I&#39;ve seen good devs get tripped up on that at first since they are writing so much error handling. So instead of an else block on something like an error in a handler, try something like...</p> <pre><code>ts, err := time.Parse(&#34;2006-01-02&#34;, query.Get(&#34;ReportDate&#34;)) if err != nil { log.Println(&#34;ReportHandler:&#34;, err) http.Error(w, &#34;Bad Request: bad start date specified (needs YYYY-MM-DD)&#34;, http.StatusBadRequest) return } //use the ReportDate timestamp... </code></pre> <p>In other cases it&#39;s better to return the error itself. In APIs I tend to add information to an error and return the error until I get to a handler and then log the error in the handler. This gives me all the information needed to understand the context around the problem.</p> <p>I adapt pretty well to different languages, so I enjoy the paradigm that Go asks me to use. I don&#39;t think people who use Go by choice see this as a shortcoming, though people who don&#39;t use Go are hesitant about it.</p> <p>I don&#39;t know what you mean about speed. I would think it would be faster than any of the other languages you know besides C++, so it&#39;s possible you have some design concerns due to misunderstanding something that you&#39;ll learn as time goes on.</p></pre>

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

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