Can two instances of the same struct be compared

Maybe you can, maybe you can’t. If the struct contains attributes of comparable types.

package main import "fmt" type TestStruct1 struct { Name string Age *int } func main(){ var age int = 18 t1 := TestStruct1{ Name: "name1", Age: &age, } t2 := TestStruct1{ Name: "name1", Age: &age, } t3 := &TestStruct1{ Name: "name1", Age: &age,} fmt.Println(t1==t2, t1== *t3, &t1 == t3)} T1 ==t2; t1== *t3; t1==t2; t1== *t3; t1==t2; t1== *t3Copy the code

A struct is not comparable if it contains attributes of non-comparable types

package main import "fmt" type TestStruct1 struct { Name string Age *int Friends []string } func main(){ var age int = 18 t1 := TestStruct1{ Name: "name1", Age: &age, Friends:[]string{"a","b"}, } t2 := TestStruct1{ Name: "name1", Age: &age, Friends:[]string{"a","b"},} FMT.Println(t1==t2)} FMT.Println(t1==t2) t1 == t2 (struct containing []string cannot be compared)Copy the code

Println(&t1==&t2), false

package main import "fmt" type TestStruct1 struct { Name string Age *int Friends []string } func main() { var age int = 18 friends := []string{"a", "b"} t1 := TestStruct1{ Name: "name1", Age: &age, Friends: friends, } t2 := TestStruct1{ Name: "name1", Age: &age, Friends: Println(&t1 == &t2, &t1 == &t3, &t1 == t4)} <br> <br> <br> <br> <br> <br> <br> <br> So &t1==&t3 is false<br> T4 is assigned with a pointer to T1, so T4 and T1 refer to the same memory address, t4 is a pointer to T1, so &t1==t4 is trueCopy the code

Can two instances of different structs be compared

Maybe you can, maybe you can’t.

package main import "fmt" type TestStruct1 struct { Name string Age *int } type TestStruct2 struct { Name string Age *int } func main() { var age int = 18 t1 := TestStruct1{ Name: "name1", Age: &age, } t2 := TestStruct2{ Name: "Name1 ", Age: &age,} fmt.Println(t1 == T2)} T1 == t2 (mismatched types TestStruct1 and TestStruct2Copy the code

Instances of two structs that are not identical cannot be compared directly, but can be compared by casting

package main import "fmt" type TestStruct1 struct { Name string Age *int } type TestStruct2 struct { Name string Age *int } func main() { var age int = 18 t1 := TestStruct1{ Name: "name1", Age: &age, } t2 := TestStruct2{ Name: "name1", Age: &age,} fmt.Println(t1 == TestStruct1(t2)) fmt.Println(TestStruct2(t1) == t2)} Although T1 and T2 are two different constructs, casting to a single structure is comparable, and the result is trueCopy the code

However, if the two structures contain attributes of non-comparable types, they cannot be compared

package main import "fmt" type TestStruct1 struct { Name string Age *int Friends []string } type TestStruct2 struct { Name string Age *int Friends []string } func main() { var age int = 18 friends := []string{"a","b"} t1 := TestStruct1{ Name: "name1", Age: &age, Friends:friends, } t2 := TestStruct2{ Name: "name1", Age: &age, Friends: Friends,} t3 := TestStruct1(t2) fmt.Println(t1 == t3)} T1 == t3 (struct containing []string cannot be compared) t2 can be cast to TestStruct1, but it cannot be compared to T1. Println(&t1==&t3) returns false because T1 and t3 are two different instancesCopy the code

Can an instance of a struct be a map key

Maybe you can, maybe you can’t. Yes if the key is comparable, but not otherwise

package main import "fmt" type TestStruct1 struct { Name string Age *int } type TestStruct2 struct { Name string Age *int Friends []string } func main() { m1 := make(map[TestStruct1]string) m2 := make(map[TestStruct2]string) M2 := make(map[TestStruct2]string) invalid map key type TestStruct2Copy the code

Sortable, comparable, not comparable

  • Sortable: int,float,string
  • Comparison: int, float, string, Boolean, interface, channel, array
  • Not comparable: slice,map,function

reference

[1] Struct in Golang can be compared