Is there a way to take an md5 value of a string x in one line?

xuanbao · · 462 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I want to take the md5 sum (I know it&#39;s almost completely broken. I need it for non-crypto code. If CRC32 would be as easy, I&#39;d use it instead).</p> <p>Right now, I do:</p> <pre><code> md5 := md5.Sum([]byte{&#39;a&#39;, &#39;b&#39;}) fmt.Println(&#34;Hello, playground&#34; + hex.EncodeToString(md5[:])) </code></pre> <p>But I&#39;d rather do that in a single line (why not?). So I plug md5 in, and get </p> <pre><code> fmt.Println(&#34;Hello, playground&#34; + hex.EncodeToString((md5.Sum([]byte{&#39;a&#39;, &#39;b&#39;}))[:])) </code></pre> <p>But it won&#39;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(&#34;%x\n&#34;, md5.Sum([]byte(&#34;ab&#34;))) </code></pre></pre>allowthere: <pre><p>I don&#39;t want to print it. I want to do something with it, and I don&#39;t think (I could be wrong) that fmt.Sptrintf is as fast as EncodeToString</p></pre>TheMerovius: <pre><p>It&#39;s always possible to do something in a single line. Whether it&#39;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. &#34;why not&#34; is not a valid reason when asking how to do something. If you think your two lines aren&#39;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, &#34;Clear is better than clever.&#34;</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&#39;t see a problem with having an intermediate value. </p></pre>allowthere: <pre><p>I&#39;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&#39;t nest. So Atoi can&#39;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&#39;ll have no choice here because of language semantics, but such a short function chain doesn&#39;t seem to merit a temp variable.</p> <p>Maybe I&#39;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&#39;d use it instead</p> </blockquote> <p>What&#39;s hard about using <a href="https://golang.org/pkg/hash/crc32/" rel="nofollow">CRC</a>?</p></pre>epiris: <pre><p>Please don&#39;t</p> <pre><code>fmt.Printf(&#34;%x&#34;,&amp;[][md5.Size]byte{md5.Sum(data)}[0]) </code></pre> <p>I&#39;m not even sure it&#39;s shorter.. lol</p></pre>

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

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