Go 中的可寻址和不可寻址怎么理解?

开发 后端
如果字典的元素不存在,则返回零值,而零值是不可变对象,如果能寻址问题就大了。而如果字典的元素存在,考虑到 Go 中 map 实现中元素的地址是变化的,这意味着寻址的结果也是无意义的。

[[427435]]

# 1. 什么叫可寻址?

可直接使用 & 操作符取地址的对象,就是可寻址的(Addressable)。比如下面这个例子

  1. func main() { 
  2.     name := "iswbm" 
  3.     fmt.Println(&name)  
  4.     // output: 0xc000010200 

程序运行不会报错,说明 name 这个变量是可寻址的。

但不能说 "iswbm" 这个字符串是可寻址的。

"iswbm" 是字符串,字符串都是不可变的,是不可寻址的,后面会介绍到。

在开始逐个介绍之前,先说一下结论

  • 指针可以寻址:&Profile{}
  • 变量可以寻址:name := Profile{}
  • 字面量通通不能寻址:Profile{}

# 2. 哪些是可以寻址的?

变量:&x

  1. func main() { 
  2.     name := "iswbm" 
  3.     fmt.Println(&name)  
  4.     // output: 0xc000010200 

指针:&*x

  1. type Profile struct { 
  2.     Name string 
  3.  
  4. func main() { 
  5.     fmt.Println(unsafe.Pointer(&Profile{Name"iswbm"})) 
  6.     // output: 0xc000108040 

数组元素索引: &a[0]

  1. func main() { 
  2.     s := [...]int{1,2,3} 
  3.     fmt.Println(&s[0]) 
  4.     // output: xc0000b4010 

切片

  1. func main() { 
  2.     fmt.Println([]int{1, 2, 3}[1:]) 

切片元素索引:&s[1]

  1. func main() { 
  2.     s := make([]int , 2, 2) 
  3.     fmt.Println(&s[0])  
  4.     // output: xc0000b4010 

组合字面量: &struct{X type}{value}

所有的组合字面量都是不可寻址的,就像下面这样子

  1. type Profile struct { 
  2.     Name string 
  3.  
  4. func new() Profile { 
  5.     return Profile{Name"iswbm"
  6.  
  7. func main() { 
  8.     fmt.Println(&new()) 
  9.     // cannot take the address of new() 

注意上面写法与这个写法的区别,下面这个写法代表不同意思,其中的 & 并不是取地址的操作,而代表实例化一个结构体的指针。

  1. type Profile struct { 
  2.     Name string 
  3.  
  4. func main() { 
  5.     fmt.Println(&Profile{Name"iswbm"}) // ok 

虽然组合字面量是不可寻址的,但却可以对组合字面量的字段属性进行寻址(直接访问)

  1. type Profile struct { 
  2.     Name string 
  3.  
  4. func new() Profile { 
  5.     return Profile{Name"iswbm"
  6.  
  7. func main() { 
  8.     fmt.Println(new().Name

# 3. 哪些是不可以寻址的?

常量

  1. import "fmt" 
  2.  
  3. const VERSION  = "1.0" 
  4.  
  5. func main() { 
  6.     fmt.Println(&VERSION) 

字符串

  1. func getStr() string { 
  2.     return "iswbm" 
  3. func main() { 
  4.     fmt.Println(&getStr()) 
  5.     // cannot take the address of getStr() 

函数或方法

  1. func getStr() string { 
  2.     return "iswbm" 
  3. func main() { 
  4.     fmt.Println(&getStr) 
  5.     // cannot take the address of getStr 

基本类型字面量

字面量分:基本类型字面量 和 复合型字面量。

基本类型字面量,是一个值的文本表示,都是不应该也是不可以被寻址的。

  1. func getInt() int { 
  2.     return 1024 
  3.  
  4. func main() { 
  5.     fmt.Println(&getInt()) 
  6.     // cannot take the address of getInt() 

map 中的元素

字典比较特殊,可以从两个角度来反向推导,假设字典的元素是可寻址的,会出现 什么问题?

如果字典的元素不存在,则返回零值,而零值是不可变对象,如果能寻址问题就大了。

而如果字典的元素存在,考虑到 Go 中 map 实现中元素的地址是变化的,这意味着寻址的结果也是无意义的。

基于这两点,Map 中的元素不可寻址,符合常理。

  1. func main() { 
  2.     p := map[string]string { 
  3.         "name""iswbm"
  4.     } 
  5.  
  6.     fmt.Println(&p["name"]) 
  7.     // cannot take the address of p["name"

搞懂了这点,你应该能够理解下面这段代码为什么会报错啦~

  1. package main 
  2.  
  3. import "fmt" 
  4.  
  5. type Person struct { 
  6.     Name  string 
  7.     Email string 
  8.  
  9. func main() { 
  10.     m := map[int]Person{ 
  11.         1:Person{"Andy""1137291867@qq.com"}, 
  12.         2:Person{"Tiny""qishuai231@gmail.com"}, 
  13.         3:Person{"Jack""qs_edu2009@163.com"}, 
  14.     } 
  15.  
  16.     //编译错误:cannot assign to struct field m[1].Name in map 
  17.     m[1].Name = "Scrapup" 

数组字面量

数组字面量是不可寻址的,当你对数组字面量进行切片操作,其实就是寻找内部元素的地址,下面这段代码是会报错的

  1. func main() { 
  2.     fmt.Println([3]int{1, 2, 3}[1:]) 
  3.     // invalid operation [3]int literal[1:] (slice of unaddressable value) 

是不是很简单?跟着明哥一起来攻克 Go 的各个边边角角的知识吧!

本文转载自微信公众号「Go编程时光」,可以通过以下二维码关注。转载本文请联系Go编程时光公众号。

 

责任编辑:武晓燕 来源: Go编程时光
相关推荐

2010-06-10 10:59:33

Internet协议

2020-09-30 06:50:35

Linux内存寻址

2017-12-28 14:51:01

AndroidView焦点

2010-03-02 17:48:35

WCF寻址报头

2015-10-09 10:22:47

分页内存寻址Linux

2015-10-08 17:25:38

分段内存寻址Linux

2024-03-18 08:50:20

分布式系统机制

2021-07-07 11:35:17

Linux内存段寻址

2013-08-14 09:11:43

云数据存储云存储云安全

2018-08-29 12:05:54

云数据存储安全

2013-07-04 09:18:39

2023-10-27 11:27:14

Go函数

2012-06-18 10:41:15

IPv6物联网

2010-05-26 15:29:30

IPv6地址

2021-03-08 08:18:53

onStar Activity 界面

2021-09-29 09:10:19

树莓派 SoCRAM寻址

2010-12-28 11:05:17

PC技术

2010-06-11 14:46:38

可路由协议

2020-10-08 18:49:47

函数可重入不可重入

2022-09-15 07:55:22

WPFMAUI内存
点赞
收藏

51CTO技术栈公众号