Should the program print true or false?

103 views
Skip to first unread message

tapi...@gmail.com

unread,
Sep 17, 2020, 11:57:59 AM9/17/20
to golang-nuts

package main

import (
  "fmt"
  "reflect"
)

func main() {
  f()
}

func f() {
  type S []S
  var a, b S
  a = S{0: b}
  b = S{0: a}
  fmt.Println(reflect.DeepEqual(a, b))
}

Now it prints false. But it looks the docs indicates it should print true.

Axel Wagner

unread,
Sep 17, 2020, 12:05:02 PM9/17/20
to tapi...@gmail.com, golang-nuts
I don't think the docs imply that. For one, a[0] is nil, and b[0] isn't.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/dbe73874-ec0f-413d-bed6-47f213bede91n%40googlegroups.com.

Axel Wagner

unread,
Sep 17, 2020, 12:05:31 PM9/17/20
to tapi...@gmail.com, golang-nuts
I think you might've intended this, which does indeed print true:
 type S []S
var a, b S
a, b = S{0: b}, S{0: a}
fmt.Println(reflect.DeepEqual(a, b))
Message has been deleted

tapi...@gmail.com

unread,
Sep 17, 2020, 12:53:45 PM9/17/20
to golang-nuts
Aha, you are totally right.
I made silly mistake here. ;D

Marvin Renich

unread,
Sep 17, 2020, 2:00:40 PM9/17/20
to golang-nuts
* 'Axel Wagner' via golang-nuts <golan...@googlegroups.com> [200917 12:05]:
> I think you might've intended this, which does indeed print true:
> type S []S
> var a, b S
> a, b = S{0: b}, S{0: a}
> fmt.Println(reflect.DeepEqual(a, b))

I was guessing he meant:

type S []S
var a, b S
a = S{0: b}
b = S{0: a}
a[0] = b
fmt.Println(reflect.DeepEqual(a, b))

which also returns true, but for a slightly different reason; check out
the following for each of the two cases:

fmt.Printf("a[0] == nil: %t, b[0] == nil: %t\n", a[0] == nil, b[0] == nil)

...Marvin

Reply all
Reply to author
Forward
0 new messages