Just getting started with go, care to review my code?

blov · · 29 次点击    
<p>I reimplemented some old code from Processing to go, and I&#39;d like to know if I&#39;m doing it right. (It&#39;s a Julia set browser/generator)</p> <p>At any rate, it&#39;s been loads of fun to be doing this.</p> <p><a href="https://github.com/mz2212/go-julia" rel="nofollow">https://github.com/mz2212/go-julia</a></p> <p>Also, how might I go about using the <code>complex128</code> type in this instead of the two <code>float64</code>s? I figure that the <code>crunch()</code> function will run faster if I use <code>complex128</code> instead.</p> <hr/>**评论:**<br/><br/>antontumasov: <pre><p>You&#39;d better use gofmt to improve readability. Now it&#39;s just a nightmare...</p></pre>TidestManager1: <pre><p>Hm. I didn&#39;t know about that utility and was just using atom&#39;s formatting.</p> <p>The more you know I suppose. Any chance you could tell me the command to use?</p></pre>: <pre><p>[deleted]</p></pre>TidestManager1: <pre><p>Huh. TIL. I&#39;ll be sure to do that with code I share in the future.</p></pre>Tetracyclic: <pre><p>It&#39;s worthwhile setting up a trigger to run <code>gofmt</code> whenever you save a .go file, it&#39;s a pretty common practice in the Go community.</p></pre>Antshockey: <pre><p>VS Code does this almost out of the box.</p></pre>allhatenocattle: <pre><p>If you are an Atom user, look at the go-plus plugin </p> <p><a href="https://atom.io/packages/go-plus" rel="nofollow">https://atom.io/packages/go-plus</a></p></pre>Redundancy_: <pre><p>I added some complex128 and pulled the julia function out. <a href="https://gist.github.com/Redundancy/a32df6a6c03304e06259ff09310ab6ed" rel="nofollow">https://gist.github.com/Redundancy/a32df6a6c03304e06259ff09310ab6ed</a></p> <p>I can&#39;t promise that it&#39;s correct or faster, but it feels a little easier to read to me. YMMV</p></pre>peterhellberg: <pre><p>I modified your version to not use the gg package:</p> <p><a href="https://gist.github.com/peterhellberg/e032fbba06fe7db42f3786252c62b23d" rel="nofollow">https://gist.github.com/peterhellberg/e032fbba06fe7db42f3786252c62b23d</a></p></pre>Redundancy_: <pre><p>Nice improvements</p></pre>TidestManager1: <pre><p>I didn&#39;t even know go had that kind of stuff in the standard libraries.</p> <p>You&#39;d think that Google of all things would be a bit better at highlighting when there&#39;s a go standard for that.</p></pre>peterhellberg: <pre><p>I did some further improvements, and render to a Pixel canvas instead of a file: <a href="https://gist.github.com/peterhellberg/6f58def911a97e839e883e3cb253c161" rel="nofollow">https://gist.github.com/peterhellberg/6f58def911a97e839e883e3cb253c161</a></p></pre>TidestManager1: <pre><p>Thank you! Good to know how the complex128 works... I wasn&#39;t really understanding the documentation for it.</p></pre>Sythe2o0: <pre><p>You&#39;ve built your own RGB struct, but golang has a lot of built in color types including RGB(A), and by using these types and the associated interfaces your code will be a lot easier to extend and understand for other people. There&#39;s a pattern for color conversion too. <a href="https://golang.org/pkg/image/color/#RGBA" rel="nofollow">https://golang.org/pkg/image/color/#RGBA</a> </p></pre>TidestManager1: <pre><p>You have a good point there. I really need to read more before I implement.</p></pre>Sythe2o0: <pre><p>This sort of thing isn&#39;t a big deal. Its just worth considering whether anything you are building could fit a standard interface.</p></pre>shovelpost: <pre><p>Use the standard tooling like <a href="https://goreportcard.com/report/github.com/mz2212/go-julia" rel="nofollow">gofmt and go lint</a>. Also <a href="http://gocover.io/github.com/mz2212/go-julia/hsvrgb" rel="nofollow">write tests</a>. There&#39;s standard tooling for <a href="https://blog.golang.org/cover" rel="nofollow">test coverage</a> as well.</p></pre>allhatenocattle: <pre><p><a href="https://github.com/mz2212/go-julia/blob/master/main.go#L13" rel="nofollow">https://github.com/mz2212/go-julia/blob/master/main.go#L13</a> </p> <p>if you want them to be constants, declare as const not var and remove the const part of the names </p> <p><a href="https://github.com/mz2212/go-julia/blob/master/main.go#L8" rel="nofollow">https://github.com/mz2212/go-julia/blob/master/main.go#L8</a><br/> <a href="https://github.com/mz2212/go-julia/blob/master/main.go#L13-L14" rel="nofollow">https://github.com/mz2212/go-julia/blob/master/main.go#L13-L14</a><br/> <a href="https://github.com/mz2212/go-julia/blob/master/main.go#L35" rel="nofollow">https://github.com/mz2212/go-julia/blob/master/main.go#L35</a> </p> <p>I find it tougher to read with multiple var/const values being set per line, I think it would be more readable to break vars with assignments out to one per line</p></pre>TidestManager1: <pre><p>Ah. In my defense, I&#39;m used to Java and Ruby where that kind of stuff seems to be the norm. My bad naming the variable const instead of declaring it as such.</p></pre>allhatenocattle: <pre><p>No worries, everyone starts writing code in a new language with a style influenced by what they have used in the past.</p> <p>There is also a great Go community on Slack if you want to ask questions in real time. </p> <p><a href="https://invite.slack.golangbridge.org/" rel="nofollow">https://invite.slack.golangbridge.org/</a></p></pre>lordoffire: <pre><p>Use <code>gofmt</code> to fix formatting/readability, and <code>go vet</code> to point out the obvious issues.</p> <ul> <li><a href="https://golang.org/cmd/vet/" rel="nofollow">https://golang.org/cmd/vet/</a></li> <li><a href="https://golang.org/cmd/gofmt/" rel="nofollow">https://golang.org/cmd/gofmt/</a></li> </ul> <p>You can also configure Atom to use the plugins to fix formatting errors / warn about issues on save.</p></pre>CountyMcCounterson: <pre><p>I&#39;m sorry guys but I just fucking can&#39;t. I&#39;ve tried to get enthusiastic about this language and look past the meme parts but come on that code looks fucking disgusting.</p> <p>Why can&#39;t the types just be before the variable name? It makes sense, it works, everyone does it. Instead you get fucking filthy lines where there&#39;s a ton of shit and then at the end it says it&#39;s an int so you have no idea until you trace it all the way to the end.</p></pre>shovelpost: <pre><blockquote> <p>It makes sense, it works</p> </blockquote> <p>Does it?</p> <p>In Go you read (and write) the code as if it was natural language. It reads very nicely. For example:</p> <pre><code>var foo string // This is variable foo which is a string. </code></pre> <p>Compare that to most other languages:</p> <pre><code>public String foo; // ??? </code></pre> <blockquote> <p>everyone does it.</p> </blockquote> <p>Well Go is different. If you want what everyone does then don&#39;t write Go.</p></pre>Sythe2o0: <pre><p>Are you referring to the lines like</p> <pre><code>var a,b,c ..., x int = 0, 0, 0 ... </code></pre> <p>Because that&#39;s not really something I see in standard Go and I&#39;d be tempted to say &#39;please don&#39;t do that, use a block instead&#39;. I don&#39;t know what gofmt says about it though.</p> <p>So in other words you&#39;re commenting on a beginner in Go doing something that is frowned upon (unless someone wants to correct me) and accusing Go of being the problem?</p></pre>
29 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet