当前位置:网站首页>Golang 类型比较
Golang 类型比较
2022-07-04 09:32:00 【动态一时爽,重构火葬场】
基本类型
基本类型比较中变量类型必须相等(即使是int、int32这种也是不能比较的)
var a int
b := 0
fmt.Println(a==b) //true
var c int32
fmt.Println(a==c) // 无法比较int与int32将在编译时报错
type INT int
var d INT
fmt.Println(a==d) // 无法比较int与INT(即使实际都是int),将在编译时报错
type aliasInt = int
var e aliasInt
fmt.Println(a==e) // 类型别名,能够比较。 true
也即
- 变量类型必须相等才能比较
- 类型再定义也不能比较
- int、int32等接近类型也不能比较
- 类型别名能够比较
数组类型
对于数组而言,要求数组中所有数据必须是可比较的,且数组长度也要一致
var a [1]string
var b [2]string
fmt.Println(a == b) // 数组长度不一致,不能比较
var c [1]int
fmt.Println(a == c) // 数组元素类型不一致,也不能比较
var d [1]string
fmt.Println(a == d) //只有数组元素类型一致,且长度相等才能比较,但
var e [1][]string
var f [1][]string
fmt.Println(e == f) // 若数组元素是不可比较类型,那么数组也不可以比较
总之对于能比较数组必须满足
- 数组长度一致
- 数组元素类型一致,且能比较
结构体类型
结构体类型只要结构成员是能比较的,那么结构体就能比较
type cmp struct {
x int
y string
}
a := cmp{
x: 0,
y: "",
}
b := cmp{
x: 0,
y: "",
}
fmt.Println(a == b) // 此时cmp结构所有成员都可以比较,因此结构之间也能比较
type notCmp struct {
x int
y []string
}
c := notCmp{
x: 0,
y: nil,
}
d := notCmp{
x: 0,
y: nil,
}
fmt.Println(c == d) // 此时nocmp结构中含有不可比较的slice类型,那么结构便不能比较
指针类型
指针类型是基本类型指针以及slice、map。对于基本类型指针比较的是指针指向的是否为同一个地址
而slice、map则无法比较
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{}类型
接口比较的是动态类型以及动态值,只有两者都相等时,比较才相等。
动态类型就是实际实现接口的结构类型,动态值就是实际实现接口的结构体
// 此时w声明为io.Writer接口,但并没有动态类型、动态值 var w io.Writer // 此时w有了动态类型*os.File,但由于f也只是个声明因此还没有动态值 var f *os.File w = f // 此时w才真的有了动态值 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和teacher尽管继承同一个接口,拥有相同的属性、方法以及值,可是他们类型不同,所以仍然是不相等的
s1 := student{
Name: name}
s2 := student{
Name: name}
fmt.Println(comparePerson(s1, s2)) // true
t1 := teacher{
Name: name}
fmt.Println(comparePerson(s1, t1)) // false
如果动态类型不能比较的话,那么也会不相等
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
函数类型
函数不可比较。
对于使用reflect.DeepEqual去硬比较,只要是两个不全是nil,返回结果都是不相等
硬要比较——slice、map
- 对于byte slice类型,可以通过bytes.Equal进行比较
- reflect.DeepEqual可以比较任意类型变量
- 当然还可以引入第三方库进行比较
Ref
- https://www.jianshu.com/p/a982807819fa
边栏推荐
- QTreeView+自定义Model实现示例
- C language - Introduction - Foundation - syntax - [main function, header file] (II)
- DR6018-CP01-wifi6-Qualcomm-IPQ6010-IPQ6018-FAMILY-2T2R-2.5G-ETH-port-CP01-802-11AX-MU-MIMO-OFDMA
- Jianzhi offer 09 realizes queue with two stacks
- Latex download installation record
- PHP is used to add, modify and delete movie information, which is divided into foreground management and background management. Foreground users can browse information and post messages, and backgroun
- What is permission? What is a role? What are users?
- Simulate EF dbcontext with MOQ - mocking EF dbcontext with MOQ
- GoLand environment variable configuration
- How to ensure the uniqueness of ID in distributed environment
猜你喜欢
How should PMP learning ideas be realized?
How to ensure the uniqueness of ID in distributed environment
Implementation principle of redis string and sorted set
PHP book borrowing management system, with complete functions, supports user foreground management and background management, and supports the latest version of PHP 7 x. Database mysql
26. Delete duplicates in the ordered array (fast and slow pointer de duplication)
Dede plug-in (multi-function integration)
2022-2028 global edible probiotic raw material industry research and trend analysis report
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
The child container margin top acts on the parent container
Mac platform forgets the root password of MySQL
随机推荐
Trim leading or trailing characters from strings- Trim leading or trailing characters from a string?
Research Report on the development trend and Prospect of global and Chinese zinc antimonide market Ⓚ 2022 ~ 2027
Review of last week's hot spots (6.27-7.3)
Deadlock in channel
Web端自动化测试失败原因汇总
20220701 barbarat lemma proof
Lauchpad X | 模式
Analysis report on the production and marketing demand and investment forecast of tellurium dioxide in the world and China Ⓣ 2022 ~ 2027
20220701 Barbalat引理证明
DR6018-CP01-wifi6-Qualcomm-IPQ6010-IPQ6018-FAMILY-2T2R-2.5G-ETH-port-CP01-802-11AX-MU-MIMO-OFDMA
Write a jison parser from scratch (2/10): learn the correct posture of the parser generator parser generator
PMP registration process and precautions
Global and Chinese markets of hemoglobin analyzers in care points 2022-2028: Research Report on technology, participants, trends, market size and share
Fatal error in golang: concurrent map writes
Les différents modèles imbriqués de listview et Pageview avec les conseils de flutter
2022-2028 global strain gauge pressure sensor industry research and trend analysis report
Reading notes on how to connect the network - hubs, routers and routers (III)
The 14th five year plan and investment risk analysis report of China's hydrogen fluoride industry 2022 ~ 2028
Jianzhi offer 09 realizes queue with two stacks
Write a jison parser (7/10) from scratch: the iterative development process of the parser generator 'parser generator'