Object equality

blov · · 396 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m trying to implement a simple <code>assertEquals</code> function. I&#39;m at the point when I don&#39;t understand how object equality works. Please have a look at this example (simplified for brevity): <a href="https://play.golang.org/p/bFv6KJcxcr8" rel="nofollow">https://play.golang.org/p/bFv6KJcxcr8</a> Why does it produce this result? I&#39;m sure this is related to the fact that the type of <code>expected</code> is <code>interface{}</code>, but I&#39;d appreciate if someone could explain this behaviour.</p> <hr/>**评论:**<br/><br/>_zombiezen_: <pre><p>What you are seeing is the same as this FAQ: <a href="https://golang.org/doc/faq#nil_error" rel="nofollow">https://golang.org/doc/faq#nil_error</a></p> <p>Slice nil is not the same as interface nil, they are two different types.</p> <p>You may want to look at something like <a href="https://github.com/google/go-cmp" rel="nofollow">https://github.com/google/go-cmp</a> for your tests instead.</p></pre>shovelpost: <pre><p>The standard library equivalent of an <code>assertEquals</code> function is to use <code>reflect.DeepEqual</code>.</p> <p>But recently the Tech Lead for Go on Cloud at Google mentioned that using <code>reflect.DeepEqual</code> can lead to <a href="https://medium.com/@zombiezen/life-of-a-go-infrastructure-maintainer-cb1419308eb5" rel="nofollow">fragile tests</a>. So... ¯\_(ツ)_/¯</p></pre>Ch00ki: <pre><p><code>reflect.DeepEqual</code> also returns <code>false</code> in this particular case.</p></pre>shovelpost: <pre><p>Well yeah because <code>[]string(nil)</code> and <code>nil</code> are different types.</p></pre>

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

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