一文理解Go 中的可尋址和不可尋址
1. 什么叫可尋址?
可直接使用 & 操作符取地址的對象,就是可尋址的(Addressable
)。比如下面這個例子
func main() { name := "iswbm" fmt.Println(&name) // output: 0xc000010200 }
程序運(yùn)行不會報錯,說明 name
這個變量是可尋址的。
但不能說 "iswbm
" 這個字符串是可尋址的。
"iswbm
" 是字符串,字符串都是不可變的,是不可尋址的,后面會介紹到。
在開始逐個介紹之前,先說一下結(jié)論
- 指針可以尋址:&Profile{}
- 變量可以尋址:name := Profile{}
- 字面量通通不能尋址:Profile{}
2. 哪些是可以尋址的?
變量:&x
func main() { name := "iswbm" fmt.Println(&name) // output: 0xc000010200 }
指針:&*x
type Profile struct { Name string } func main() { fmt.Println(unsafe.Pointer(&Profile{Name: "iswbm"})) // output: 0xc000108040 }
數(shù)組元素索引: &a[0]
func main() { s := [...]int{1,2,3} fmt.Println(&s[0]) // output: xc0000b4010 }
切片
func main() { fmt.Println([]int{1, 2, 3}[1:]) }
切片元素索引:&s[1]
func main() { s := make([]int , 2, 2) fmt.Println(&s[0]) // output: xc0000b4010 }
組合字面量: &struct{X type}{value}
所有的組合字面量都是不可尋址的,就像下面這樣子
type Profile struct { Name string } func new() Profile { return Profile{Name: "iswbm"} } func main() { fmt.Println(&new()) // cannot take the address of new() }
注意上面寫法與這個寫法的區(qū)別,下面這個寫法代表不同意思,其中的 & 并不是取地址的操作,而代表實(shí)例化一個結(jié)構(gòu)體的指針。
type Profile struct { Name string } func main() { fmt.Println(&Profile{Name: "iswbm"}) // ok }
雖然組合字面量是不可尋址的,但卻可以對組合字面量的字段屬性進(jìn)行尋址(直接訪問)
type Profile struct { Name string } func new() Profile { return Profile{Name: "iswbm"} } func main() { fmt.Println(new().Name) }
3. 哪些是不可以尋址的?
常量
import "fmt" const VERSION = "1.0" func main() { fmt.Println(&VERSION) }
字符串
func getStr() string { return "iswbm" } func main() { fmt.Println(&getStr()) // cannot take the address of getStr() }
函數(shù)或方法
func getStr() string { return "iswbm" } func main() { fmt.Println(&getStr) // cannot take the address of getStr }
基本類型字面量
字面量分:基本類型字面量 和 復(fù)合型字面量。
基本類型字面量,是一個值的文本表示,都是不應(yīng)該也是不可以被尋址的。
func getInt() int { return 1024 } func main() { fmt.Println(&getInt()) // cannot take the address of getInt() }
map 中的元素
字典比較特殊,可以從兩個角度來反向推導(dǎo),假設(shè)字典的元素是可尋址的,會出現(xiàn) 什么問題?
如果字典的元素不存在,則返回零值,而零值是不可變對象,如果能尋址問題就大了。
而如果字典的元素存在,考慮到 Go 中 map
實(shí)現(xiàn)中元素的地址是變化的,這意味著尋址的結(jié)果也是無意義的。
基于這兩點(diǎn),Map 中的元素不可尋址,符合常理。
func main() { p := map[string]string { "name": "iswbm", } fmt.Println(&p["name"]) // cannot take the address of p["name"] }
搞懂了這點(diǎn),你應(yīng)該能夠理解下面這段代碼為什么會報錯啦~
package main import "fmt" type Person struct { Name string Email string } func main() { m := map[int]Person{ 1:Person{"Andy", "1137291867@qq.com"}, 2:Person{"Tiny", "qishuai231@gmail.com"}, 3:Person{"Jack", "qs_edu2009@163.com"}, } //編譯錯誤:cannot assign to struct field m[1].Name in map m[1].Name = "Scrapup"
數(shù)組字面量
數(shù)組字面量是不可尋址的,當(dāng)你對數(shù)組字面量進(jìn)行切片操作,其實(shí)就是尋找內(nèi)部元素的地址,下面這段代碼是會報錯的
func main() { fmt.Println([3]int{1, 2, 3}[1:]) // invalid operation [3]int literal[1:] (slice of unaddressable value) }
到此這篇關(guān)于一文理解Go 中的可尋址和不可尋址的文章就介紹到這了,更多相關(guān)Go 中可尋址和不可尋址內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang中crypto/cipher加密標(biāo)準(zhǔn)庫全面指南
本文主要介紹了Golang中crypto/cipher加密標(biāo)準(zhǔn)庫,包括對稱加密、非對稱加密以及使用流加密和塊加密算法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02golang中判斷請求是http還是https獲取當(dāng)前訪問地址
這篇文章主要為大家介紹了golang中判斷請求是http還是https獲取當(dāng)前訪問地址示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10執(zhí)行g(shù)o?vendor第三方包版本沖突問題解決
這篇文章主要為大家介紹了執(zhí)行g(shù)o?vendor時,第三方包go版本沖突問題的解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Golang 實(shí)現(xiàn)插入排序的方法示例(2種)
這篇文章主要介紹了Golang 實(shí)現(xiàn)插入排序的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02gin自定義中間件解決requestBody不可重復(fù)讀問題(最新推薦)
這篇文章主要介紹了gin自定義中間件解決requestBody不可重復(fù)讀問題,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04Golang設(shè)計(jì)模式之原型模式詳細(xì)講解
如果一個類的有非常多的屬性,層級還很深。每次構(gòu)造起來,不管是直接構(gòu)造還是用建造者模式,都要對太多屬性進(jìn)行復(fù)制,那么有沒有一種好的方式讓我們創(chuàng)建太的時候使用體驗(yàn)更好一點(diǎn)呢? 今天的文章里就給大家介紹一種設(shè)計(jì)模式,來解決這個問題2023-01-01Go 并發(fā)控制context實(shí)現(xiàn)原理剖析(小結(jié))
Golang context是Golang應(yīng)用開發(fā)常用的并發(fā)控制技術(shù),這篇文章主要介紹了Go 并發(fā)控制context實(shí)現(xiàn)原理剖析(小結(jié)),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10