in conclusion
In golang, which data types are comparable and which are incomparable:
- Comparable: Integer, Floating-point, String, Boolean, Complex (plural), Pointer, Channel, Interface, Array
- Not comparable: Slice, Map, Function
Can two instances of the same struct be compared
type S struct { Name string Age int Address *int } func main() { a := S{ Name: "aa", Age: 1, Address: new(int), } b := S{ Name: "aa", Age: 1, Address: new(int), } (a == b) // false }
Output false, description:
- It can be compared because they are all basic data types
- false: Because the value of the pointer variable Address is different, a != b. If a b removes Address during initialization (not initializing Address), then a == b is true at this time, because the default value of the ptr variable is nil, or the value of the same pointer variable is assigned to the Address member variable is also valid.
type S struct { Name string Age int Address *int Data []int } func main() { a := S{ Name: "aa", Age: 1, Address: new(int), Data: []int{1, 2, 3}, } b := S{ Name: "aa", Age: 1, Address: new(int), Data: []int{1, 2, 3}, } (a == b) // invalid operation: a == b (struct containing []int cannot be compared) }
Error: Slice, Map, Function cannot be compared
Recommended usage of structure
type S struct { Name string Age int Address *int Data []int } func main() { a := S{ Name: "aa", Age: 1, Address: new(int), Data: []int{1, 2, 3}, } b := S{ Name: "aa", Age: 1, Address: new(int), Data: []int{1, 2, 3}, } ((a, b)) // true }
The DeepEqual function is used to determine whether the depth of the two values is consistent. The specific comparison rules are as follows:
- Different types of values will never be equal in depth
- When the corresponding depths of two arrays are equal, the depths of the two arrays are equal.
- When all fields of two identical structures correspond to the same depth, the depths of the two structures are equal.
- When both functions are nil, the depths of the two functions are equal, and other cases are not equal (the same functions are not equal either)
- When the true depths of two interfaces are equal, the depths of the two interfaces are equal.
- Comparison of maps requires the following to meet the following
- - Both maps are nil or neither are nil, and the lengths must be equal
- -The same map object or all keys must correspond to the same
- -The value corresponding to map must also have equal depth.
- Pointer, satisfying one of the following is equal depth
- -The == operator of the two pointers satisfy the go == operator
- -The values pointed to by two pointers are of equal depth
- Slicing requires the following points to be met at the same time to be equal in depth
- - Both slices are nil or neither are nil and have equal length
- -The first position pointed to by the underlying data of the two slices must be the same or the underlying elements must be equal in depth.
- - Note: The empty slice and nil slice are not equal in depth
- Other types of values (numbers, bools, strings, channels) are of equal depth if the == operator of go. Note that not all values are equal to their depth, such as functions, as well as nested structures, arrays, etc. containing these values.
This is the end of this article about how to determine the equality of two structures in Golang. For more related content on judging that the equality of two structures, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!