if the object is created by the same struct without complex type can be directly used by == on the ratio and pointer
Simple type
sortable data type
Integer
Floating point
String
data type that can be compared
in addition to the above three, there are
Boolean,
Complex,
Pointer,
Channel,
Interface
Array
complex non-comparable data type
Slice
Map
Function
p> as follows
type User struct {
age int
name string
}
func main() {
user := User{
1,
"d",
}
user2 := User{
1,
"d",
}
fmt.Println(user == user2) //打印的结果是true 会去自动对比内部的属性是否相等
//但是如果结构体内部含有map,slice,Function 使用==比较编译会报错
}
the == operator
examplecan be used if two objects that are not created in the same structure can be converted to each other and do not contain non-comparable member variables
type USER struct {
age int
name string
u Name
}
type Name struct {
a string
}
type USER2 struct {
age int
name string
u Name
}
func main() {
user := USER{
1,
"d",
Name{""},
}
user3 := USER2{
1,
"d",
Name{""},
}
user2 := USER(user3)
fmt.Println(user2 == user) //编译通过 打印结果是true
}
refer to official document
Read More:
- How to solve the problem of failed installation of golang plug-in dependency in vscode under Windows
- How to get all the keys of map by golang
- Compare whether two sets are the same in Java
- Solve the problem of failure in installation of golang plug-in dependency in vscode
- Golang gets the list of files under the folder
- Golang determines whether the directory is empty
- Golang timer function executes a function every few minutes
- How to set the custom blood bar UI in UE4 and how to call it
- macbook golang zsh: exec format error
- [Solved] Golang Error: The system cannot find the path specified. [mkdir C:/xx/yy/]:
- Golang Warning: Error string should not be capitalized or end with punctuation mark
- How to print how to output Int64_ t,uint64_ The value of T in C
- How to deal with NullPointerException error in row.getlong in spark – sparksql
- How to Fix ”is not in the sudoers file“ in Linux
- How to wrap a long string in MATLAB
- How to call stored procedure in Hibernate
- How to save big data in Oracle to CLOB
- express nodejs Failed to lookup view error in views directory (How to Fix)
- The @ Autowired annotation in springboot is invalid in ordinary classes. How to solve and use the null pointer exception java.lang.nullpointerexception
- Fatal error: MSpanList_Insert error when installing golang source code