How to compare struct, slice, map are equal?












75















I want to check that two structs are equal, but have some problem:



package main

import (
"fmt"
"reflect"
)

type T struct {
X int
Y string
Z int
M map[string]int
}

func main() {
t1 := T{
X:1,
Y:"lei",
Z:int{1,2,3},
M:map[string]int{
"a":1,
"b":2,
},
}

t2 := T{
X:1,
Y:"lei",
Z:int{1,2,3},
M:map[string]int{
"a":1,
"b":2,
},
}


fmt.Println(t2 == t1)
//error - invalid operation: t2 == t1 (struct containing int cannot be compared)


fmt.Println(reflect.ValueOf(t2) == reflect.ValueOf(t1))
//false
fmt.Println(reflect.TypeOf(t2) == reflect.TypeOf(t1))
//true


//Update: slice or map
a1 := int{1,2,3,4}
a2 := int{1,2,3,4}

fmt.Println(a1==a2)
//invalid operation: a1 == a2 (slice can only be compared to nil)

m1 := map[string]int{
"a":1,
"b":2,
}
m2 := map[string]int{
"a":1,
"b":2,
}
fmt.Println(m1==m2)
// m1 == m2 (map can only be compared to nil)
}


http://play.golang.org/p/AZIzW2WunI










share|improve this question

























  • COnsider also 'invalid operation: t2 == t1 (struct containing map[string]int cannot be compared)', this happens if the struct has no int within his definition

    – Victor
    Apr 4 '17 at 14:21
















75















I want to check that two structs are equal, but have some problem:



package main

import (
"fmt"
"reflect"
)

type T struct {
X int
Y string
Z int
M map[string]int
}

func main() {
t1 := T{
X:1,
Y:"lei",
Z:int{1,2,3},
M:map[string]int{
"a":1,
"b":2,
},
}

t2 := T{
X:1,
Y:"lei",
Z:int{1,2,3},
M:map[string]int{
"a":1,
"b":2,
},
}


fmt.Println(t2 == t1)
//error - invalid operation: t2 == t1 (struct containing int cannot be compared)


fmt.Println(reflect.ValueOf(t2) == reflect.ValueOf(t1))
//false
fmt.Println(reflect.TypeOf(t2) == reflect.TypeOf(t1))
//true


//Update: slice or map
a1 := int{1,2,3,4}
a2 := int{1,2,3,4}

fmt.Println(a1==a2)
//invalid operation: a1 == a2 (slice can only be compared to nil)

m1 := map[string]int{
"a":1,
"b":2,
}
m2 := map[string]int{
"a":1,
"b":2,
}
fmt.Println(m1==m2)
// m1 == m2 (map can only be compared to nil)
}


http://play.golang.org/p/AZIzW2WunI










share|improve this question

























  • COnsider also 'invalid operation: t2 == t1 (struct containing map[string]int cannot be compared)', this happens if the struct has no int within his definition

    – Victor
    Apr 4 '17 at 14:21














75












75








75


19






I want to check that two structs are equal, but have some problem:



package main

import (
"fmt"
"reflect"
)

type T struct {
X int
Y string
Z int
M map[string]int
}

func main() {
t1 := T{
X:1,
Y:"lei",
Z:int{1,2,3},
M:map[string]int{
"a":1,
"b":2,
},
}

t2 := T{
X:1,
Y:"lei",
Z:int{1,2,3},
M:map[string]int{
"a":1,
"b":2,
},
}


fmt.Println(t2 == t1)
//error - invalid operation: t2 == t1 (struct containing int cannot be compared)


fmt.Println(reflect.ValueOf(t2) == reflect.ValueOf(t1))
//false
fmt.Println(reflect.TypeOf(t2) == reflect.TypeOf(t1))
//true


//Update: slice or map
a1 := int{1,2,3,4}
a2 := int{1,2,3,4}

fmt.Println(a1==a2)
//invalid operation: a1 == a2 (slice can only be compared to nil)

m1 := map[string]int{
"a":1,
"b":2,
}
m2 := map[string]int{
"a":1,
"b":2,
}
fmt.Println(m1==m2)
// m1 == m2 (map can only be compared to nil)
}


http://play.golang.org/p/AZIzW2WunI










share|improve this question
















I want to check that two structs are equal, but have some problem:



package main

import (
"fmt"
"reflect"
)

type T struct {
X int
Y string
Z int
M map[string]int
}

func main() {
t1 := T{
X:1,
Y:"lei",
Z:int{1,2,3},
M:map[string]int{
"a":1,
"b":2,
},
}

t2 := T{
X:1,
Y:"lei",
Z:int{1,2,3},
M:map[string]int{
"a":1,
"b":2,
},
}


fmt.Println(t2 == t1)
//error - invalid operation: t2 == t1 (struct containing int cannot be compared)


fmt.Println(reflect.ValueOf(t2) == reflect.ValueOf(t1))
//false
fmt.Println(reflect.TypeOf(t2) == reflect.TypeOf(t1))
//true


//Update: slice or map
a1 := int{1,2,3,4}
a2 := int{1,2,3,4}

fmt.Println(a1==a2)
//invalid operation: a1 == a2 (slice can only be compared to nil)

m1 := map[string]int{
"a":1,
"b":2,
}
m2 := map[string]int{
"a":1,
"b":2,
}
fmt.Println(m1==m2)
// m1 == m2 (map can only be compared to nil)
}


http://play.golang.org/p/AZIzW2WunI







go go-reflect






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 22 '16 at 5:24







user6169399

















asked Jul 2 '14 at 14:41









leiyonglinleiyonglin

2,14292536




2,14292536













  • COnsider also 'invalid operation: t2 == t1 (struct containing map[string]int cannot be compared)', this happens if the struct has no int within his definition

    – Victor
    Apr 4 '17 at 14:21



















  • COnsider also 'invalid operation: t2 == t1 (struct containing map[string]int cannot be compared)', this happens if the struct has no int within his definition

    – Victor
    Apr 4 '17 at 14:21

















COnsider also 'invalid operation: t2 == t1 (struct containing map[string]int cannot be compared)', this happens if the struct has no int within his definition

– Victor
Apr 4 '17 at 14:21





COnsider also 'invalid operation: t2 == t1 (struct containing map[string]int cannot be compared)', this happens if the struct has no int within his definition

– Victor
Apr 4 '17 at 14:21












3 Answers
3






active

oldest

votes


















109














You can use reflect.DeepEqual, or you can implement your own function (which performance wise would be better than using reflection):



http://play.golang.org/p/CPdfsYGNy_



m1 := map[string]int{   
"a":1,
"b":2,
}
m2 := map[string]int{
"a":1,
"b":2,
}
fmt.Println(reflect.DeepEqual(m1, m2))





share|improve this answer































    26














    reflect.DeepEqual is often incorrectly used to compare two like structs, as in your question.



    cmp.Equal is a better tool for comparing structs.



    To see why reflection is ill-advised, let's look at the documentation:




    Struct values are deeply equal if their corresponding fields, both exported and unexported, are deeply equal.



    ....



    numbers, bools, strings, and channels - are deeply equal if they are equal using Go's == operator.




    If we compare two time.Time values of the same UTC time, t1 == t2 will be false if they're metadata timezone is different.



    go-cmp looks for the Equal() method and uses that to correctly compare times.



    Example:



    m1 := map[string]int{
    "a": 1,
    "b": 2,
    }
    m2 := map[string]int{
    "a": 1,
    "b": 2,
    }
    fmt.Println(cmp.Equal(m1, m2)) // will result in true





    share|improve this answer





















    • 3





      Yes exactly! When writing tests, it's very important to use go-cmp and not reflect.

      – Kevin Minehart
      Sep 19 '17 at 1:01











    • Unfortunately neither reflect nor cmp work for comparing a struct with a slice of pointers to structs. It still wants the pointers to be the same.

      – GeneralLeeSpeaking
      Oct 26 '18 at 21:35








    • 1





      @GeneralLeeSpeaking that's not true. From the cmp documentation: "Pointers are equal if the underlying values they point to are also equal"

      – Ilia Choly
      Dec 29 '18 at 18:11





















    14














    Here's how you'd roll your own function http://play.golang.org/p/Qgw7XuLNhb



    func compare(a, b T) bool {
    if &a == &b {
    return true
    }
    if a.X != b.X || a.Y != b.Y {
    return false
    }
    if len(a.Z) != len(b.Z) || len(a.M) != len(b.M) {
    return false
    }
    for i, v := range a.Z {
    if b.Z[i] != v {
    return false
    }
    }
    for k, v := range a.M {
    if b.M[k] != v {
    return false
    }
    }
    return true
    }





    share|improve this answer





















    • 3





      I'd recommend adding if len(a.Z) != len(b.Z) || len(a.M) != len(b.M) { return false }, because one of them could have extra fields.

      – OneOfOne
      Jul 2 '14 at 15:04






    • 2





      Here's my version of this : play.golang.org/p/mzvvoksjDq

      – OneOfOne
      Jul 2 '14 at 15:10













    • @OneOfOne I think you posted the wrong link.

      – Ilia Choly
      Jul 2 '14 at 15:11






    • 2





      @Rick-777 there's simply no comparison defined for slices. This is how the language designers wanted it to be. It's not as simple to define as, say, comparison of simple integers. Are slices equal if they contain same elements in the same order? But what if their capacities differ? Etc.

      – justinas
      Jul 12 '14 at 15:45








    • 1





      if &a == &b { return true } This will never evaluate to true if the parameters to compare are being passed by value.

      – Sean
      Feb 16 '18 at 1:48











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24534072%2fhow-to-compare-struct-slice-map-are-equal%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    109














    You can use reflect.DeepEqual, or you can implement your own function (which performance wise would be better than using reflection):



    http://play.golang.org/p/CPdfsYGNy_



    m1 := map[string]int{   
    "a":1,
    "b":2,
    }
    m2 := map[string]int{
    "a":1,
    "b":2,
    }
    fmt.Println(reflect.DeepEqual(m1, m2))





    share|improve this answer




























      109














      You can use reflect.DeepEqual, or you can implement your own function (which performance wise would be better than using reflection):



      http://play.golang.org/p/CPdfsYGNy_



      m1 := map[string]int{   
      "a":1,
      "b":2,
      }
      m2 := map[string]int{
      "a":1,
      "b":2,
      }
      fmt.Println(reflect.DeepEqual(m1, m2))





      share|improve this answer


























        109












        109








        109







        You can use reflect.DeepEqual, or you can implement your own function (which performance wise would be better than using reflection):



        http://play.golang.org/p/CPdfsYGNy_



        m1 := map[string]int{   
        "a":1,
        "b":2,
        }
        m2 := map[string]int{
        "a":1,
        "b":2,
        }
        fmt.Println(reflect.DeepEqual(m1, m2))





        share|improve this answer













        You can use reflect.DeepEqual, or you can implement your own function (which performance wise would be better than using reflection):



        http://play.golang.org/p/CPdfsYGNy_



        m1 := map[string]int{   
        "a":1,
        "b":2,
        }
        m2 := map[string]int{
        "a":1,
        "b":2,
        }
        fmt.Println(reflect.DeepEqual(m1, m2))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 2 '14 at 14:53









        OneOfOneOneOfOne

        61k10119139




        61k10119139

























            26














            reflect.DeepEqual is often incorrectly used to compare two like structs, as in your question.



            cmp.Equal is a better tool for comparing structs.



            To see why reflection is ill-advised, let's look at the documentation:




            Struct values are deeply equal if their corresponding fields, both exported and unexported, are deeply equal.



            ....



            numbers, bools, strings, and channels - are deeply equal if they are equal using Go's == operator.




            If we compare two time.Time values of the same UTC time, t1 == t2 will be false if they're metadata timezone is different.



            go-cmp looks for the Equal() method and uses that to correctly compare times.



            Example:



            m1 := map[string]int{
            "a": 1,
            "b": 2,
            }
            m2 := map[string]int{
            "a": 1,
            "b": 2,
            }
            fmt.Println(cmp.Equal(m1, m2)) // will result in true





            share|improve this answer





















            • 3





              Yes exactly! When writing tests, it's very important to use go-cmp and not reflect.

              – Kevin Minehart
              Sep 19 '17 at 1:01











            • Unfortunately neither reflect nor cmp work for comparing a struct with a slice of pointers to structs. It still wants the pointers to be the same.

              – GeneralLeeSpeaking
              Oct 26 '18 at 21:35








            • 1





              @GeneralLeeSpeaking that's not true. From the cmp documentation: "Pointers are equal if the underlying values they point to are also equal"

              – Ilia Choly
              Dec 29 '18 at 18:11


















            26














            reflect.DeepEqual is often incorrectly used to compare two like structs, as in your question.



            cmp.Equal is a better tool for comparing structs.



            To see why reflection is ill-advised, let's look at the documentation:




            Struct values are deeply equal if their corresponding fields, both exported and unexported, are deeply equal.



            ....



            numbers, bools, strings, and channels - are deeply equal if they are equal using Go's == operator.




            If we compare two time.Time values of the same UTC time, t1 == t2 will be false if they're metadata timezone is different.



            go-cmp looks for the Equal() method and uses that to correctly compare times.



            Example:



            m1 := map[string]int{
            "a": 1,
            "b": 2,
            }
            m2 := map[string]int{
            "a": 1,
            "b": 2,
            }
            fmt.Println(cmp.Equal(m1, m2)) // will result in true





            share|improve this answer





















            • 3





              Yes exactly! When writing tests, it's very important to use go-cmp and not reflect.

              – Kevin Minehart
              Sep 19 '17 at 1:01











            • Unfortunately neither reflect nor cmp work for comparing a struct with a slice of pointers to structs. It still wants the pointers to be the same.

              – GeneralLeeSpeaking
              Oct 26 '18 at 21:35








            • 1





              @GeneralLeeSpeaking that's not true. From the cmp documentation: "Pointers are equal if the underlying values they point to are also equal"

              – Ilia Choly
              Dec 29 '18 at 18:11
















            26












            26








            26







            reflect.DeepEqual is often incorrectly used to compare two like structs, as in your question.



            cmp.Equal is a better tool for comparing structs.



            To see why reflection is ill-advised, let's look at the documentation:




            Struct values are deeply equal if their corresponding fields, both exported and unexported, are deeply equal.



            ....



            numbers, bools, strings, and channels - are deeply equal if they are equal using Go's == operator.




            If we compare two time.Time values of the same UTC time, t1 == t2 will be false if they're metadata timezone is different.



            go-cmp looks for the Equal() method and uses that to correctly compare times.



            Example:



            m1 := map[string]int{
            "a": 1,
            "b": 2,
            }
            m2 := map[string]int{
            "a": 1,
            "b": 2,
            }
            fmt.Println(cmp.Equal(m1, m2)) // will result in true





            share|improve this answer















            reflect.DeepEqual is often incorrectly used to compare two like structs, as in your question.



            cmp.Equal is a better tool for comparing structs.



            To see why reflection is ill-advised, let's look at the documentation:




            Struct values are deeply equal if their corresponding fields, both exported and unexported, are deeply equal.



            ....



            numbers, bools, strings, and channels - are deeply equal if they are equal using Go's == operator.




            If we compare two time.Time values of the same UTC time, t1 == t2 will be false if they're metadata timezone is different.



            go-cmp looks for the Equal() method and uses that to correctly compare times.



            Example:



            m1 := map[string]int{
            "a": 1,
            "b": 2,
            }
            m2 := map[string]int{
            "a": 1,
            "b": 2,
            }
            fmt.Println(cmp.Equal(m1, m2)) // will result in true






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 3 '17 at 20:02









            snap

            2,2751531




            2,2751531










            answered Jul 20 '17 at 18:24









            Cole BittelCole Bittel

            78011122




            78011122








            • 3





              Yes exactly! When writing tests, it's very important to use go-cmp and not reflect.

              – Kevin Minehart
              Sep 19 '17 at 1:01











            • Unfortunately neither reflect nor cmp work for comparing a struct with a slice of pointers to structs. It still wants the pointers to be the same.

              – GeneralLeeSpeaking
              Oct 26 '18 at 21:35








            • 1





              @GeneralLeeSpeaking that's not true. From the cmp documentation: "Pointers are equal if the underlying values they point to are also equal"

              – Ilia Choly
              Dec 29 '18 at 18:11
















            • 3





              Yes exactly! When writing tests, it's very important to use go-cmp and not reflect.

              – Kevin Minehart
              Sep 19 '17 at 1:01











            • Unfortunately neither reflect nor cmp work for comparing a struct with a slice of pointers to structs. It still wants the pointers to be the same.

              – GeneralLeeSpeaking
              Oct 26 '18 at 21:35








            • 1





              @GeneralLeeSpeaking that's not true. From the cmp documentation: "Pointers are equal if the underlying values they point to are also equal"

              – Ilia Choly
              Dec 29 '18 at 18:11










            3




            3





            Yes exactly! When writing tests, it's very important to use go-cmp and not reflect.

            – Kevin Minehart
            Sep 19 '17 at 1:01





            Yes exactly! When writing tests, it's very important to use go-cmp and not reflect.

            – Kevin Minehart
            Sep 19 '17 at 1:01













            Unfortunately neither reflect nor cmp work for comparing a struct with a slice of pointers to structs. It still wants the pointers to be the same.

            – GeneralLeeSpeaking
            Oct 26 '18 at 21:35







            Unfortunately neither reflect nor cmp work for comparing a struct with a slice of pointers to structs. It still wants the pointers to be the same.

            – GeneralLeeSpeaking
            Oct 26 '18 at 21:35






            1




            1





            @GeneralLeeSpeaking that's not true. From the cmp documentation: "Pointers are equal if the underlying values they point to are also equal"

            – Ilia Choly
            Dec 29 '18 at 18:11







            @GeneralLeeSpeaking that's not true. From the cmp documentation: "Pointers are equal if the underlying values they point to are also equal"

            – Ilia Choly
            Dec 29 '18 at 18:11













            14














            Here's how you'd roll your own function http://play.golang.org/p/Qgw7XuLNhb



            func compare(a, b T) bool {
            if &a == &b {
            return true
            }
            if a.X != b.X || a.Y != b.Y {
            return false
            }
            if len(a.Z) != len(b.Z) || len(a.M) != len(b.M) {
            return false
            }
            for i, v := range a.Z {
            if b.Z[i] != v {
            return false
            }
            }
            for k, v := range a.M {
            if b.M[k] != v {
            return false
            }
            }
            return true
            }





            share|improve this answer





















            • 3





              I'd recommend adding if len(a.Z) != len(b.Z) || len(a.M) != len(b.M) { return false }, because one of them could have extra fields.

              – OneOfOne
              Jul 2 '14 at 15:04






            • 2





              Here's my version of this : play.golang.org/p/mzvvoksjDq

              – OneOfOne
              Jul 2 '14 at 15:10













            • @OneOfOne I think you posted the wrong link.

              – Ilia Choly
              Jul 2 '14 at 15:11






            • 2





              @Rick-777 there's simply no comparison defined for slices. This is how the language designers wanted it to be. It's not as simple to define as, say, comparison of simple integers. Are slices equal if they contain same elements in the same order? But what if their capacities differ? Etc.

              – justinas
              Jul 12 '14 at 15:45








            • 1





              if &a == &b { return true } This will never evaluate to true if the parameters to compare are being passed by value.

              – Sean
              Feb 16 '18 at 1:48
















            14














            Here's how you'd roll your own function http://play.golang.org/p/Qgw7XuLNhb



            func compare(a, b T) bool {
            if &a == &b {
            return true
            }
            if a.X != b.X || a.Y != b.Y {
            return false
            }
            if len(a.Z) != len(b.Z) || len(a.M) != len(b.M) {
            return false
            }
            for i, v := range a.Z {
            if b.Z[i] != v {
            return false
            }
            }
            for k, v := range a.M {
            if b.M[k] != v {
            return false
            }
            }
            return true
            }





            share|improve this answer





















            • 3





              I'd recommend adding if len(a.Z) != len(b.Z) || len(a.M) != len(b.M) { return false }, because one of them could have extra fields.

              – OneOfOne
              Jul 2 '14 at 15:04






            • 2





              Here's my version of this : play.golang.org/p/mzvvoksjDq

              – OneOfOne
              Jul 2 '14 at 15:10













            • @OneOfOne I think you posted the wrong link.

              – Ilia Choly
              Jul 2 '14 at 15:11






            • 2





              @Rick-777 there's simply no comparison defined for slices. This is how the language designers wanted it to be. It's not as simple to define as, say, comparison of simple integers. Are slices equal if they contain same elements in the same order? But what if their capacities differ? Etc.

              – justinas
              Jul 12 '14 at 15:45








            • 1





              if &a == &b { return true } This will never evaluate to true if the parameters to compare are being passed by value.

              – Sean
              Feb 16 '18 at 1:48














            14












            14








            14







            Here's how you'd roll your own function http://play.golang.org/p/Qgw7XuLNhb



            func compare(a, b T) bool {
            if &a == &b {
            return true
            }
            if a.X != b.X || a.Y != b.Y {
            return false
            }
            if len(a.Z) != len(b.Z) || len(a.M) != len(b.M) {
            return false
            }
            for i, v := range a.Z {
            if b.Z[i] != v {
            return false
            }
            }
            for k, v := range a.M {
            if b.M[k] != v {
            return false
            }
            }
            return true
            }





            share|improve this answer















            Here's how you'd roll your own function http://play.golang.org/p/Qgw7XuLNhb



            func compare(a, b T) bool {
            if &a == &b {
            return true
            }
            if a.X != b.X || a.Y != b.Y {
            return false
            }
            if len(a.Z) != len(b.Z) || len(a.M) != len(b.M) {
            return false
            }
            for i, v := range a.Z {
            if b.Z[i] != v {
            return false
            }
            }
            for k, v := range a.M {
            if b.M[k] != v {
            return false
            }
            }
            return true
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 18 '14 at 4:19

























            answered Jul 2 '14 at 15:00









            Ilia CholyIlia Choly

            10.5k966128




            10.5k966128








            • 3





              I'd recommend adding if len(a.Z) != len(b.Z) || len(a.M) != len(b.M) { return false }, because one of them could have extra fields.

              – OneOfOne
              Jul 2 '14 at 15:04






            • 2





              Here's my version of this : play.golang.org/p/mzvvoksjDq

              – OneOfOne
              Jul 2 '14 at 15:10













            • @OneOfOne I think you posted the wrong link.

              – Ilia Choly
              Jul 2 '14 at 15:11






            • 2





              @Rick-777 there's simply no comparison defined for slices. This is how the language designers wanted it to be. It's not as simple to define as, say, comparison of simple integers. Are slices equal if they contain same elements in the same order? But what if their capacities differ? Etc.

              – justinas
              Jul 12 '14 at 15:45








            • 1





              if &a == &b { return true } This will never evaluate to true if the parameters to compare are being passed by value.

              – Sean
              Feb 16 '18 at 1:48














            • 3





              I'd recommend adding if len(a.Z) != len(b.Z) || len(a.M) != len(b.M) { return false }, because one of them could have extra fields.

              – OneOfOne
              Jul 2 '14 at 15:04






            • 2





              Here's my version of this : play.golang.org/p/mzvvoksjDq

              – OneOfOne
              Jul 2 '14 at 15:10













            • @OneOfOne I think you posted the wrong link.

              – Ilia Choly
              Jul 2 '14 at 15:11






            • 2





              @Rick-777 there's simply no comparison defined for slices. This is how the language designers wanted it to be. It's not as simple to define as, say, comparison of simple integers. Are slices equal if they contain same elements in the same order? But what if their capacities differ? Etc.

              – justinas
              Jul 12 '14 at 15:45








            • 1





              if &a == &b { return true } This will never evaluate to true if the parameters to compare are being passed by value.

              – Sean
              Feb 16 '18 at 1:48








            3




            3





            I'd recommend adding if len(a.Z) != len(b.Z) || len(a.M) != len(b.M) { return false }, because one of them could have extra fields.

            – OneOfOne
            Jul 2 '14 at 15:04





            I'd recommend adding if len(a.Z) != len(b.Z) || len(a.M) != len(b.M) { return false }, because one of them could have extra fields.

            – OneOfOne
            Jul 2 '14 at 15:04




            2




            2





            Here's my version of this : play.golang.org/p/mzvvoksjDq

            – OneOfOne
            Jul 2 '14 at 15:10







            Here's my version of this : play.golang.org/p/mzvvoksjDq

            – OneOfOne
            Jul 2 '14 at 15:10















            @OneOfOne I think you posted the wrong link.

            – Ilia Choly
            Jul 2 '14 at 15:11





            @OneOfOne I think you posted the wrong link.

            – Ilia Choly
            Jul 2 '14 at 15:11




            2




            2





            @Rick-777 there's simply no comparison defined for slices. This is how the language designers wanted it to be. It's not as simple to define as, say, comparison of simple integers. Are slices equal if they contain same elements in the same order? But what if their capacities differ? Etc.

            – justinas
            Jul 12 '14 at 15:45







            @Rick-777 there's simply no comparison defined for slices. This is how the language designers wanted it to be. It's not as simple to define as, say, comparison of simple integers. Are slices equal if they contain same elements in the same order? But what if their capacities differ? Etc.

            – justinas
            Jul 12 '14 at 15:45






            1




            1





            if &a == &b { return true } This will never evaluate to true if the parameters to compare are being passed by value.

            – Sean
            Feb 16 '18 at 1:48





            if &a == &b { return true } This will never evaluate to true if the parameters to compare are being passed by value.

            – Sean
            Feb 16 '18 at 1:48


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24534072%2fhow-to-compare-struct-slice-map-are-equal%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word