package main
import "fmt"
type N []N
func main() {
var n N = make(N, 1)
n[0] = n
fmt.Println(n)
}Why arraies can be compared but not slices?
package main
func main() {
if [2]int{1,2} == [2]int{1,2} {
println("equal array")
}
if []int{1,2} == []int{1,2} {
On Apr 21, 5:06 pm, minux <minux...@gmail.com> wrote:
> On Sat, Apr 21, 2012 at 11:58 PM, Archos <raul....@sent.com> wrote:There is a faster way:
> > Why arraies can be compared but not slices?
> > package main
> > func main() {
> > if [2]int{1,2} == [2]int{1,2} {
> > println("equal array")
> > }
> > if []int{1,2} == []int{1,2} {
>
> For example, to make this comparison works as you expected, the program
> must traverse the slice and compare one by one, as shown, this could lead to
> endless loop.
slice1 := []int{1,2}
slice2 := []int{1,2}
strSlice1 := fmt.Sprintf("%v", slice1)
Then, using json:
strSlice1, _ := json.Marshal(slice1)
strSlice2, _ := json.Marshal(slice2)
if string(strSlice1) == string(strSlice2) {
println("equal slice")
}
although it follows without working with slices self referential.
No, lisp systems have long known how to reliably do deep compare or copy of recursive structures without a loop.
The more serious problem is that, since slices are reference types, it is unclear whether equality should be the same segment of the same underlying array, or simply equality of elements.
Thomas
No, lisp systems have long known how to reliably do deep compare or copy of recursive structures without a loop.