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

大家好,我是明哥。

本專欄內容,已經上傳 github:https://github.com/iswbm/golang-interview

請大家幫幫忙去點個小 ⭐⭐,在那裏我對題庫進行了分類整理。

本篇問題:Go 中的可尋址和不可尋址怎麼理解?

1. 什麼叫可尋址?


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

func main() {
    name := "iswbm"
    fmt.Println(&name) 
    // output: 0xc000010200
}

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

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

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

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

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
}

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

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

type Profile struct {
    Name string
}

func main() {
    fmt.Println(&Profile{Name: "iswbm"}) // ok
}

雖然組合字面量是不可尋址的,但卻可以對組合字面量的字段屬性進行尋址(直接訪問)

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

 函數或方法

func getStr() string {
    return "iswbm"
}
func main() {
    fmt.Println(&getStr)
    // cannot take the address of getStr
}

 基本類型字面量

字面量分:基本類型字面量複合型字面量

基本類型字面量,是一個值的文本表示,都是不應該也是不可以被尋址的。

func getInt() int {
    return 1024
}

func main() {
    fmt.Println(&getInt())
    // cannot take the address of getInt()
}

 map 中的元素

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

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

  2. 而如果字典的元素存在,考慮到 Go 中 map 實現中元素的地址是變化的,這意味着尋址的結果也是無意義的。

基於這兩點,Map 中的元素不可尋址,符合常理。

func main() {
    p := map[string]string {
        "name""iswbm",
    }

    fmt.Println(&p["name"])
    // cannot take the address of p["name"]
}

搞懂了這點,你應該能夠理解下面這段代碼爲什麼會報錯啦~

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"

 數組字面量

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

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

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

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/aeTvhcM4o5d3kPibkKwWOg