当前位置:网站首页>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

  1. Boolean( Boolean value )、Integer( integer )、Floating-point( Floating point numbers )、Complex( The plural )、String( character ) These types are undoubtedly comparable .
  2. 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 .
  3. 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
  1. Interface ( Interface value ) Have comparability : If two interface values have the same dynamic type and equal dynamic values , Then they are equal .
  2. 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 .
  3. 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 .
 Insert picture description here
Reference material

原网站

版权声明
本文为[It artist rookie]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090407035066.html