FAQ
Hi,

Is there any reason why

     reflect.DeepEqual([]byte(""), []byte(nil)) -> false

while

     bytes.Bytes([]byte(""), []byte(nil)) -> true

? You can also check it here: http://play.golang.org/p/yAH9oFWChr .

This is quite inconsistent I must say, I've just spent quite some time before I realized this…

Regards,
Ondrej Kupka

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Atkaaz at Aug 24, 2013 at 3:24 pm
    interesting
    http://play.golang.org/p/Ze10kz4Sqr


    On Sat, Aug 24, 2013 at 4:52 PM, Ondrej Kupka wrote:

    Hi,

    Is there any reason why

    reflect.DeepEqual([]byte(""), []byte(nil)) -> false

    while

    bytes.Bytes([]byte(""), []byte(nil)) -> true

    ? You can also check it here: http://play.golang.org/p/yAH9oFWChr .

    This is quite inconsistent I must say, I've just spent quite some time
    before I realized this…

    Regards,
    Ondrej Kupka

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brad Fitzpatrick at Aug 24, 2013 at 7:05 pm
    These are both documented.

    reflect.DeepEqual: "An empty slice is not equal to a nil slice."
    bytes.Equal: "A nil argument is equivalent to an empty slice."

    In the types of places you use each, this is what you almost always want.


    On Sat, Aug 24, 2013 at 9:52 AM, Ondrej Kupka wrote:

    Hi,

    Is there any reason why

    reflect.DeepEqual([]byte(""), []byte(nil)) -> false

    while

    bytes.Bytes([]byte(""), []byte(nil)) -> true

    ? You can also check it here: http://play.golang.org/p/yAH9oFWChr .

    This is quite inconsistent I must say, I've just spent quite some time
    before I realized this…

    Regards,
    Ondrej Kupka

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ondřej Kupka at Aug 25, 2013 at 8:16 am
    Hi,
    On Saturday, August 24, 2013 9:05:45 PM UTC+2, bradfitz wrote:

    These are both documented.
    Yes, I've noticed, a bit too late, but whatever :-)

    reflect.DeepEqual: "An empty slice is not equal to a nil slice."
    bytes.Equal: "A nil argument is equivalent to an empty slice."

    In the types of places you use each, this is what you almost always want.
    I was obviously expecting DeepEqual to work as recursive bytes.Equal
    in case I want to compare two [][]byte. And I was like what the hell?
    They have the same content, but then I had it printed by each []byte
    and [] != [], so they are not equal with DeepEqual, because
    []byte(nil) != []byte("") :-)

    So I guess I have to do it myself []byte by []byte...

    Regards,
    Ondrej Kupka


    On Sat, Aug 24, 2013 at 9:52 AM, Ondrej Kupka <ondr...@gmail.com<javascript:>
    wrote:
    Hi,

    Is there any reason why

    reflect.DeepEqual([]byte(""), []byte(nil)) -> false

    while

    bytes.Bytes([]byte(""), []byte(nil)) -> true

    ? You can also check it here: http://play.golang.org/p/yAH9oFWChr .

    This is quite inconsistent I must say, I've just spent quite some time
    before I realized this…

    Regards,
    Ondrej Kupka

    --
    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 <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Roger peppe at Aug 25, 2013 at 2:53 pm

    On 24 August 2013 20:05, Brad Fitzpatrick wrote:
    These are both documented.

    reflect.DeepEqual: "An empty slice is not equal to a nil slice."

    bytes.Equal: "A nil argument is equivalent to an empty slice."

    In the types of places you use each, this is what you almost always want.
    I've found that the DeepEqual behaviour is almost never what
    I want - it's awkward when testing and you have to depend
    on the subtle difference between:

    var x []string
    for ... {
         x = append(x, foo)
    }

    and

    x := []string{}
    for ... {
          x = append(x, foo)
    }

    I've been thinking of making a version of reflect.DeepEqual that
    compares a nil slice equal to an empty slice, just to get around
    that awkwardness.

    To be honest, I also wish that a nil slice marshalled as json [] not null
    for similar reasons.

    Of course, we can't change either of these now...

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Aram Hăvărneanu at Aug 25, 2013 at 3:04 pm

    I've found that the DeepEqual behaviour is almost never what
    I want - it's awkward when testing and you have to depend
    on the subtle difference between: [ snip ]
    Code in Juju made use of this subtle distinction (not sure if it's
    still the case). I always found it unfortunate.

    --
    Aram Hăvărneanu

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 24, '13 at 2:53p
activeAug 25, '13 at 3:04p
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase