当前位置:网站首页>Golang --- comparison operation of various types of variables
Golang --- comparison operation of various types of variables
2022-06-09 04:08:00 【It artist rookie】
section
var arr []float32
fmt.Printf("%p&arr=\n", arr)
arr = append(arr, []float32{
11.0, 22, 12, 24}...)
fmt.Printf("%parr=\n", arr)
fmt.Println("&arr=", &arr[0])
fmt.Println("&arr=", &arr[1])
fmt.Println("&arr=", &arr[2])
fmt.Println("&arr=", &arr[3])
fmt.Printf("%parr=\n", arr)
var arr2 []float32
arr2 = append(arr2, []float32{
11.0, 22, 12, 24}...)
fmt.Println("&arr2=", &arr2[0])
fmt.Println("&arr2=", &arr2[1])
fmt.Println("&arr2=", &arr2[2])
fmt.Println("&arr2=", &arr2[3])
fmt.Println(reflect.DeepEqual(arr, arr2))
//0x0&arr=
// 0xc00009e220arr=
// &arr= 0xc00009e220
//&arr= 0xc00009e224
//&arr= 0xc00009e228
//&arr= 0xc00009e22c
//0xc00009e220arr=
//0xc00000c2e0arr2=
// &arr2= 0xc00009e230
//&arr2= 0xc00009e234
//&arr2= 0xc00009e238
//&arr2= 0xc00009e23c
//true
First , Out-of-service ”==“ To compare variables of slice type , To use reflect.DeepEqual(), then arr Address and arr2 The address of is different , If according to Java Is equal to the old symbol comparison , It must be unequal , Here, the cover method is used to compare the return true, That is, they are equal in content , This method assumes that they are equal
- Boolean( Boolean value )、Integer( integer )、Floating-point( Floating point numbers )、Complex( The plural )、String( character ) These types are undoubtedly comparable .
- Poniter ( The pointer ) You can compare : If two pointers point to the same variable , Or two pointers with the same type and values nil, Then they are equal . Be careful , Pointers to different zero size variables may be equal , It may not be equal .
- Channel ( passageway ) Have comparability : If two channel values are determined by the same make Call to create , Then they are equal .
c1 := make(chan int, 2)
c2 := make(chan int, 2)
c3 := c1
fmt.Println(c3 == c1) // true
fmt.Println(c2 == c1) // false
- Interface ( Interface value ) Have comparability : If two interface values have the same dynamic type and equal dynamic values , Then they are equal .
- When type X The values of are comparable and X Realization T when , Non interface type X Value x And interface type T Value t Have comparability . If t The dynamic type and X Same and t The dynamic value of is equal to x, Then they are equal .
- If all fields are comparable , be struct ( Structure value ) Have comparability : If their corresponding non empty fields are equal , Then the values of the two structures are equal .
If array( Array ) The values of element types are comparable , The array values are comparable : If their corresponding elements are equal , Then the two array values are equal .
Which types are not comparable ?
slice、map、function These are not comparable , But there are special cases , That is when they are worth nil when , It can be done with nil Compare .
We come to a conclusion : If our variables contain non comparable types , perhaps interface type ( Its dynamic type may not be comparable ), So let's use the comparison operator directly == , Will cause a program error . At this time, we should choose reflect.DeepEqual function ( Of course, there are special cases , for example []byte, Can pass bytes. Equal function Compare ).
Incomparable types include slice、map、function, They cannot be used == Compare . Although we can pass == Operator pairs interface Compare , Because of the existence of dynamic types , If you realize interface Of T There are incomparable types , This will cause runtime errors .
In case of uncertainty interface In the case of the implementation type of , Yes interface Comparison , have access to reflect.DeepEqual function .
Last , We go through json In the process of parsing and anti parsing of the library , Found out json There are data type conversion operations for parsing .
Reference material
边栏推荐
- Erreur! Il n'y a pas de texte de style spécifié dans le document. (1)
- ACM tutorial - Hill sort
- Rigidbody2d SweepTest Rigidbody2D.Cast 但用于rotation 旋转
- [word] error! There is no text of the specified style in the document. one
- Face detection, training and recognition based on OpenCV
- Principle analysis of the bottom plate of Ruixin micro rk3399 development board
- Binary processing of opcv image
- 目标检测模型mAP计算方法与对比步骤——对比原模型与量化模型之间的mAP
- 【优秀毕设】基于OpenCV的人脸识别打卡/签到/考勤管理系统(最简基本库开发、可基于树莓派)
- Kubernetes binary installation (v1.20.16) (IV) deployment master
猜你喜欢

CPU surge problem location

From just entering the testing industry to doubling my salary: talk about my advanced testing experience, which is worth learning from

1264_FreeRTOS任务的初始化以及堆栈初始化处理分析

1264_ Analysis of FreeRTOS task initialization and stack initialization processing

Dart: Foundation

Software testing (II)

Apple Announces Winner of the 2022 Apple Design Award

丰富的色彩变化

全国大学生信息安全竞赛(CISCN)-reverse-复现(部分)

测试网站搭建+渗透+审计之第三篇Swagger接口渗透测试
随机推荐
人才缺口50万以上,平均薪资20K?网络安全,测试员的下一个风口~
知识点滴- BC和BCE的区别
目标检测模型mAP计算方法与对比步骤——对比原模型与量化模型之间的mAP
在线摩斯密码在线翻译转换工具
Solve the problem that the MySQL library or table is stuck during deletion and other operations
几何运用题
【LeetCode】第48天 - 1037. 有效的回旋镖
测试网站搭建+渗透+审计之第三篇Swagger接口渗透测试
用户控制 键盘鼠标自定义 脚本
php反序列化复现——bugku 点login没反应
[examination in May] Oracle OCP 19C passed
Getting started with Maui custom drawing
状态模式模拟电梯运行
Assembly: CPU structure - flag register and related instructions
Pdf merge based on pyqt5
Erreur! Il n'y a pas de texte de style spécifié dans le document. (1)
Test website construction + penetration + audit Part II penetration test
[excellent design] opencv based face recognition punch in / sign in / attendance management system (the simplest basic library development, which can be based on raspberry pie)
(三)VGG复现
接口性能测试:web service接口测试