<p>I'm trying to compare to slices to see if they're the same .. slice. <em>Not the values</em> within the slice, but if they're the same slice. Upon trying <code>a == b</code> i found out Go doesn't allow that.</p>
<p>Eg: My goal is something like this code:</p>
<pre><code>a := []int{1,2}
b := a
c := []int{1,2}
if a == b {
// I know they're the same slice, do stuff.
}
</code></pre>
<p>I would expect <code>a == c</code> to fail, because they're different slices entirely, even though they have the same values.</p>
<p>Thoughts?</p>
<hr/>**评论:**<br/><br/>014a: <pre><pre><code>reflect.ValueOf(a).Pointer()
</code></pre>
<p>Might work. </p>
<p>Its a weird request though. Not that your use-case is weird, but that its something "weird" in the context of Go. Slices are views into arrays, so something like:</p>
<pre><code>a := []int{1,2}
a1 := a[:]
b := a
b1 := b[:]
</code></pre>
<p>Never actually "do" anything. There's no copying. There's no new data. All of those assigned variables point to the exact same data in memory.</p></pre>TheMerovius: <pre><pre><code>len(a) == len(b) && (len(a) == 0 || &a[0] == &b[0])
</code></pre>
<p>Note, that this will consider all length-zero slices to be equivalent, which makes sense, given what <a href="https://golang.org/ref/spec#Size_and_alignment_guarantees" rel="nofollow">the spec has to say</a> about that:</p>
<blockquote>
<p>Two distinct zero-size variables may have the same address in memory.</p>
</blockquote>
<p>(i.e. the address in a zero-length slice may or may not be the same)</p></pre>lhxtx: <pre><p><code>reflect.DeepEqual</code>?</p></pre>sOundsoFpErsistence: <pre><p>That compares values, does it not?</p></pre>singron: <pre><p>You can compare their lengths and a pointer to their elements. I.e.</p>
<pre><code>len(a) == len(b) && (len(a) == 0 || &a[0] == &b[0])
</code></pre>
<p>This assumes that 0 length slices are identical, but that might not be true if they have unused non-0 capacity. It probably doesn't matter since <code>append</code> can always do a reallocation, so you probably arent interested in the identity 0 length slices.</p></pre>jerf: <pre><p>For fun, <a href="https://play.golang.org/p/r7Q0M3I1l5M" rel="nofollow">see this playground entry</a>.</p>
<p>Even so, I wouldn't use this, because while it establishes that the two slices in question are <em>currently</em> pointing to the exact same thing, they're still not identically the same, like they would be in some languages. Notice in this <a href="https://play.golang.org/p/q_QiYTejlog" rel="nofollow">very similar code</a> that when I pass the same slice in to a function twice, and append to one parameter but not the other, that the slices are now different, and have different pointers in the function. Slices don't have the kind of identity I suspect you are looking for, because the <a href="https://golang.org/pkg/reflect/#SliceHeader" rel="nofollow">three fields of a slice</a> are copied by value when they are passed into a function.</p>
<p>If you want it, you'll have to impose it yourself:</p>
<pre><code>type MySlice struct {
S []int
}
</code></pre>
<p>Now <code>&MySlice</code> can serve as an identity cleanly. (Technically you can <code>type MySlice *[]int</code>, and then that pointer can be the identity, but while I'm not sure I'm ready to call that "Un-Go-ic", it would make a lot more future readers of your code go "Bwuh?" in a way that the previous type will not.)</p>
<p>Even so, I would <em>still</em> suspect you of playing games with Go that you probably shouldn't. Perhaps you should give us a hint of why you want to do this?</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传