自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 后端
如果字典的元素不存在,則返回零值,而零值是不可變對象,如果能尋址問題就大了。而如果字典的元素存在,考慮到 Go 中 map 實現(xiàn)中元素的地址是變化的,這意味著尋址的結(jié)果也是無意義的。

[[427435]]

# 1. 什么叫可尋址?

可直接使用 & 操作符取地址的對象,就是可尋址的(Addressable)。比如下面這個例子

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

程序運行不會報錯,說明 name 這個變量是可尋址的。

但不能說 "iswbm" 這個字符串是可尋址的。

"iswbm" 是字符串,字符串都是不可變的,是不可尋址的,后面會介紹到。

在開始逐個介紹之前,先說一下結(jié)論

  • 指針可以尋址:&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 

數(shù)組元素索引: &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() 

注意上面寫法與這個寫法的區(qū)別,下面這個寫法代表不同意思,其中的 & 并不是取地址的操作,而代表實例化一個結(jié)構體的指針。

  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() 

函數(shù)或方法

  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 中的元素

字典比較特殊,可以從兩個角度來反向推導,假設字典的元素是可尋址的,會出現(xiàn) 什么問題?

如果字典的元素不存在,則返回零值,而零值是不可變對象,如果能尋址問題就大了。

而如果字典的元素存在,考慮到 Go 中 map 實現(xiàn)中元素的地址是變化的,這意味著尋址的結(jié)果也是無意義的。

基于這兩點,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" 

數(shù)組字面量

數(shù)組字面量是不可尋址的,當你對數(shù)組字面量進行切片操作,其實就是尋找內(nèi)部元素的地址,下面這段代碼是會報錯的

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

是不是很簡單?跟著明哥一起來攻克 Go 的各個邊邊角角的知識吧!

本文轉(zhuǎn)載自微信公眾號「Go編程時光」,可以通過以下二維碼關注。轉(zhuǎn)載本文請聯(lián)系Go編程時光公眾號。

 

責任編輯:武曉燕 來源: Go編程時光
相關推薦

2010-06-10 10:59:33

Internet協(xié)議

2020-09-30 06:50:35

Linux內(nèi)存尋址

2017-12-28 14:51:01

AndroidView焦點

2010-03-02 17:48:35

WCF尋址報頭

2015-10-09 10:22:47

分頁內(nèi)存尋址Linux

2015-10-08 17:25:38

分段內(nèi)存尋址Linux

2024-03-18 08:50:20

分布式系統(tǒng)機制

2013-08-14 09:11:43

云數(shù)據(jù)存儲云存儲云安全

2021-07-07 11:35:17

Linux內(nèi)存段尋址

2018-08-29 12:05:54

云數(shù)據(jù)存儲安全

2013-07-04 09:18:39

2012-06-18 10:41:15

IPv6物聯(lián)網(wǎng)

2010-05-26 15:29:30

IPv6地址

2023-10-27 11:27:14

Go函數(shù)

2024-11-26 08:55:51

RustPanic

2021-09-29 09:10:19

樹莓派 SoCRAM尋址

2021-03-08 08:18:53

onStar Activity 界面

2010-12-28 11:05:17

PC技術

2024-10-16 09:57:52

空結(jié)構體map屬性

2022-09-15 07:55:22

WPFMAUI內(nèi)存
點贊
收藏

51CTO技術棧公眾號