当前位置:网站首页>Golang type comparison
Golang type comparison
2022-07-04 09:39:00 【Dynamic for a while, reconstructing the crematorium】
Basic types
Variable types must be equal in basic type comparison ( Even if it's int、int32 This is also incomparable )
var a int
b := 0
fmt.Println(a==b) //true
var c int32
fmt.Println(a==c) // Can't compare int And int32 Errors will be reported in the compilation
type INT int
var d INT
fmt.Println(a==d) // Can't compare int And INT( Even if it is int), Errors will be reported in the compilation
type aliasInt = int
var e aliasInt
fmt.Println(a==e) // Type the alias , Can compare . true
That is to say
- Variable types must be equal to compare
- Types cannot be compared again
- int、int32 Similar types cannot be compared
- Type aliases can be compared
An array type
For arrays , All data in the array must be comparable , And the array length should also be consistent
var a [1]string
var b [2]string
fmt.Println(a == b) // Inconsistent array length , Can't compare
var c [1]int
fmt.Println(a == c) // Array element types are inconsistent , It can't be compared
var d [1]string
fmt.Println(a == d) // Only the array element types are consistent , And the same length can be compared , but
var e [1][]string
var f [1][]string
fmt.Println(e == f) // If array elements are of the non comparable type , Then arrays cannot be compared
In short, arrays that can be compared must meet
- The array length is the same
- The array element types are consistent , And can compare
Type of structure
Structure type as long as the structure members are comparable , Then the structure can be compared
type cmp struct {
x int
y string
}
a := cmp{
x: 0,
y: "",
}
b := cmp{
x: 0,
y: "",
}
fmt.Println(a == b) // here cmp All members of the structure can be compared , Therefore, the structures can also be compared
type notCmp struct {
x int
y []string
}
c := notCmp{
x: 0,
y: nil,
}
d := notCmp{
x: 0,
y: nil,
}
fmt.Println(c == d) // here nocmp The structure contains incomparable slice type , Then the structure cannot be compared
Pointer types
Pointer types are basic type pointers and slice、map. For basic types, the pointer compares whether the pointer points to the same address
and slice、map There is no comparison
var o int
a := &o
b := &o
fmt.Println(a == b) // true
var o1 int
c := &o1
fmt.Println(a == c) // false
type simple struct {
x int
y string
}
d := &simple{
}
f := &simple{
}
g := d
fmt.Println(d == f) // false
fmt.Println(d == g) // true
interface{} type
Interfaces compare dynamic types and dynamic values , Only when both are equal , Comparison is equal .
Dynamic types are the structural types that actually implement interfaces , Dynamic value is the structure that actually implements the interface
// here w Declare as io.Writer Interface , But there is no dynamic type 、 Dynamic values var w io.Writer // here w With dynamic types *os.File, But because of f It is just a declaration, so there is no dynamic value var f *os.File w = f // here w Only then really had the dynamic value w = &os.File{ }
type Person interface {
getName() string
}
type student struct {
Name string
}
type teacher struct {
Name string
}
func (s student) getName() string {
return s.Name
}
func (t teacher) getName() string {
return t.Name
}
const name = "xiaoming"
func comparePerson(p1, p2 Person) bool {
return p1 == p2
}
// student and teacher Although inherit the same interface , Have the same properties 、 Methods and values , But they are different , So it is still unequal
s1 := student{
Name: name}
s2 := student{
Name: name}
fmt.Println(comparePerson(s1, s2)) // true
t1 := teacher{
Name: name}
fmt.Println(comparePerson(s1, t1)) // false
If dynamic types cannot be compared , Then it will not be equal
type mapStudent map[string]string
type mapTeacher map[string]string
func (m mapStudent) getName() string {
return m["key"]
}
func (m mapTeacher) getName() string {
return m["key"]
}
ms1 := mapStudent{
"key": name,
}
ms2 := mapTeacher{
"key": name,
}
fmt.Println(comparePerson(ms1, ms2)) // false
Function type
Functions are not comparable .
For the use of reflect.DeepEqual Go hard and compare , As long as it's two, it's not all nil, The returned results are not equal
Insist on comparison ——slice、map
- about byte slice type , Can pass bytes.Equal Compare
- reflect.DeepEqual You can compare variables of any type
- Of course, third-party libraries can also be introduced for comparison
Ref
- https://www.jianshu.com/p/a982807819fa
边栏推荐
- "How to connect the network" reading notes - Web server request and response (4)
- Function comparison between cs5261 and ag9310 demoboard test board | cost advantage of cs5261 replacing ange ag9310
- Hands on deep learning (32) -- fully connected convolutional neural network FCN
- el-table单选并隐藏全选框
- 2022-2028 global edible probiotic raw material industry research and trend analysis report
- How to ensure the uniqueness of ID in distributed environment
- Kubernetes CNI 插件之Fabric
- Reading notes on how to connect the network - hubs, routers and routers (III)
- C语言指针经典面试题——第一弹
- 什么是uid?什么是Auth?什么是验证器?
猜你喜欢
H5 audio tag custom style modification and adding playback control events
ASP. Net to access directory files outside the project website
C语言指针经典面试题——第一弹
How to batch change file extensions in win10
Four common methods of copying object attributes (summarize the highest efficiency)
华为联机对战如何提升玩家匹配成功几率
Latex download installation record
智能网关助力提高工业数据采集和利用
Sort out the power node, Mr. Wang he's SSM integration steps
HMS core helps baby bus show high-quality children's digital content to global developers
随机推荐
lolcat
After unplugging the network cable, does the original TCP connection still exist?
Global and Chinese market of planar waveguide optical splitter 2022-2028: Research Report on technology, participants, trends, market size and share
PHP personal album management system source code, realizes album classification and album grouping, as well as album image management. The database adopts Mysql to realize the login and registration f
2022-2028 global special starch industry research and trend analysis report
Fatal error in golang: concurrent map writes
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
What is permission? What is a role? What are users?
品牌连锁店5G/4G无线组网方案
Analysis report on the production and marketing demand and investment forecast of tellurium dioxide in the world and China Ⓣ 2022 ~ 2027
How does idea withdraw code from remote push
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
智能网关助力提高工业数据采集和利用
Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache
Write a jison parser from scratch (4/10): detailed explanation of the syntax format of the jison parser generator
C # use gdi+ to add text to the picture and make the text adaptive to the rectangular area
Reading notes on how to connect the network - hubs, routers and routers (III)
H5 audio tag custom style modification and adding playback control events
Write a jison parser from scratch (3/10): a good beginning is half the success -- "politics" (Aristotle)