<p>I want to take the md5 sum (I know it's almost completely broken. I need it for non-crypto code. If CRC32 would be as easy, I'd use it instead).</p>
<p>Right now, I do:</p>
<pre><code> md5 := md5.Sum([]byte{'a', 'b'})
fmt.Println("Hello, playground" + hex.EncodeToString(md5[:]))
</code></pre>
<p>But I'd rather do that in a single line (why not?). So I plug md5 in, and get </p>
<pre><code> fmt.Println("Hello, playground" + hex.EncodeToString((md5.Sum([]byte{'a', 'b'}))[:]))
</code></pre>
<p>But it won't compile, complaining of </p>
<pre><code>invalid operation md5.Sum([]byte literal)[:] (slice of unaddressable value)
</code></pre>
<p><a href="https://play.golang.org/p/dy7aemajWa" rel="nofollow">https://play.golang.org/p/dy7aemajWa</a></p>
<p>Is there a way to shorten it?</p>
<hr/>**评论:**<br/><br/>Redundancy_: <pre><p>Make a function?</p></pre>plectid: <pre><pre><code>fmt.Printf("%x\n", md5.Sum([]byte("ab")))
</code></pre></pre>allowthere: <pre><p>I don't want to print it. I want to do something with it, and I don't think (I could be wrong) that fmt.Sptrintf is as fast as EncodeToString</p></pre>TheMerovius: <pre><p>It's always possible to do something in a single line. Whether it's possible to do it in a single statement, or expression is a different question.</p>
<p>But it also seems a misguided goal to me. "why not" is not a valid reason when asking how to do something. If you think your two lines aren't readable enough, make a function <code>hexMD5Sum</code> or whatever and use that. Purely minimizing the number of lines is not a contest that go is designed to win at.</p></pre>ChristophBerger: <pre><p>Remember the Go proverb, "Clear is better than clever."</p></pre>allowthere: <pre><p>I may be wrong, but something like hex.EncodeToString(md5.Sum(myByteArray)) seems simpler than using intermediate values. </p></pre>ChristophBerger: <pre><p>It may be simpler but it is not clearer. The two lines perform two different tasks: The first one does an md5 calculation, the second one turns the result into textual output. Clear separation of concerns.</p>
<p>The combined line mingles these two logically separate steps together. Simpler maybe, as it saves an intermediate value, but less clear IMO.</p>
<p>And I don't see a problem with having an intermediate value. </p></pre>allowthere: <pre><p>I'm not an expert in Go, but I create a temp variable when:</p>
<ol>
<li><p>You use it in two places.</p></li>
<li><p>You have no choice (your first function returns two variables, so you can't nest. So Atoi can't be nested directly).</p></li>
<li><p>You have deep nesting. So a(b(c(d),f(g),h(i,j))) (did I match the parentheses? ) is much less clear than</p>
<p>temp1 := c(d)
temp2 := f(g)
temp3 := h(i,j)
temp4 := b(temp1,temp2,temp3)
a (temp4)</p></li>
</ol>
<p>but </p>
<pre><code>a := XtoA(CtoX(c))
</code></pre>
<p>is much clearer than </p>
<pre><code>temp := CtoX(c)
a := XtoA(temp)
</code></pre>
<p>It could be that I'll have no choice here because of language semantics, but such a short function chain doesn't seem to merit a temp variable.</p>
<p>Maybe I'll ask it as an independent question on the Reddit.</p></pre>ChristophBerger: <pre><p>I understand your reasoning. But the one-liner you came up with <em>does</em> have 4 levels of parentheses, so your point 3 seems to apply here.</p>
<p>But anyway, try to avoid micro-optimization, you just lose time and gain almost nothing. Two lines vs one line; a temp var vs no temp var - this is far less important than finally shipping code that works (and is maintainable, of course).</p></pre>metamatic: <pre><blockquote>
<p>If CRC32 would be as easy, I'd use it instead</p>
</blockquote>
<p>What's hard about using <a href="https://golang.org/pkg/hash/crc32/" rel="nofollow">CRC</a>?</p></pre>epiris: <pre><p>Please don't</p>
<pre><code>fmt.Printf("%x",&[][md5.Size]byte{md5.Sum(data)}[0])
</code></pre>
<p>I'm not even sure it's shorter.. lol</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传