go語言裡面receiver應該定義成一個值還是指標

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

go語言裡面receiver應該定義成一個值還是指標,比如

func (s *MyStruct) pointerMethod() { } // method on pointerfunc (s MyStruct)  valueMethod()   { } // method on value

哪種定義方式是滿足需求的。

先舉一個代碼例子

package mainimport "fmt"type MyStruct struct { i1, i2, i3, i4 int }func (h * MyStruct) incI2() { h.i2++ }func (h   MyStruct) incI3() { h.i3++ }func main() {  h1 := MyStruct{0x1111, 0x2222, 0x3333, 0x4444}  h2 := &h1  h1.i1 = 11111111  h1.incI2()  h1.i1 = 22222222  h1.incI3()  h1.i1 = 33333333  h2.incI2()  h2.incI3()  fmt.Printf("i2=%x, i3=%x\n", h1.i2, h2.i3)}

運行結果為

$ go build main.go && ./main i2=2224, i3=3333

可以看到i2被加了兩次, i3的值沒有變化; 究其原因是因為incI2()方法的聲明裡面receiver是指標, 而incI3()是值。

FAQ裡面有一個解答,作為值和作為指標的差異。https://golang.org/doc/faq#methods_on_values_or_pointers
思想很簡單,就是我們把receiver看作一個參數,看你需要這個參數是傳值,還是傳指標。

func pointerMethod(s *MyStruct) { } // passing parameter as pointerfunc valueMethod(s MyStruct)   { } // passing parameter as value

下面屬於附件內容;看一下具體代碼是怎麼處理receiver作為值和作為指標的。

先看作為指標 incI2

caller的指令如下

    0x008a 00138 (/path/to/main.go:15) LEAQ    "".h1+64(SP), AX    0x008f 00143 (/path/to/main.go:15) MOVQ    AX, (SP)    0x0093 00147 (/path/to/main.go:15) PCDATA  $0, $1    0x0093 00147 (/path/to/main.go:15) CALL    "".(*MyStruct).incI2(SB)

看到指令把h1的地址,傳給AX,然後壓入棧頂,再call inc2.

看callee(incI2)的代碼

    0x0000 00000 (/path/to/main.go:7)  TEXT    "".(*MyStruct).incI2(SB), $16-8    0x0000 00000 (/path/to/main.go:7)  SUBQ    $16, SP    0x0004 00004 (/path/to/main.go:7)  MOVQ    BP, 8(SP)    0x0009 00009 (/path/to/main.go:7)  LEAQ    8(SP), BP    0x000e 00014 (/path/to/main.go:7)  FUNCDATA    $0, gclocals·a36216b97439c93dafebe03e7f0808b5(SB)    0x000e 00014 (/path/to/main.go:7)  FUNCDATA    $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)    0x000e 00014 (/path/to/main.go:7)  MOVQ    "".h+24(FP), AX    0x0013 00019 (/path/to/main.go:7)  TESTB   AL, (AX)    0x0015 00021 (/path/to/main.go:7)  MOVQ    8(AX), AX    0x0019 00025 (/path/to/main.go:7)  MOVQ    AX, ""..autotmp_0(SP)    0x001d 00029 (/path/to/main.go:7)  MOVQ    "".h+24(FP), CX    0x0022 00034 (/path/to/main.go:7)  TESTB   AL, (CX)    0x0024 00036 (/path/to/main.go:7)  INCQ    AX    0x0027 00039 (/path/to/main.go:7)  MOVQ    AX, 8(CX)    0x002b 00043 (/path/to/main.go:7)  MOVQ    8(SP), BP    0x0030 00048 (/path/to/main.go:7)  ADDQ    $16, SP    0x0034 00052 (/path/to/main.go:7)  RET

再看作為值 incI3

caller的指令如下

    0x00a1 00161 (/path/to/main.go:17) MOVQ    "".h1+64(SP), AX    0x00a6 00166 (/path/to/main.go:17) MOVQ    "".h1+72(SP), CX    0x00ab 00171 (/path/to/main.go:17) MOVQ    "".h1+80(SP), DX    0x00b0 00176 (/path/to/main.go:17) MOVQ    "".h1+88(SP), BX    0x00b5 00181 (/path/to/main.go:17) MOVQ    AX, (SP)    0x00b9 00185 (/path/to/main.go:17) MOVQ    CX, 8(SP)    0x00be 00190 (/path/to/main.go:17) MOVQ    DX, 16(SP)    0x00c3 00195 (/path/to/main.go:17) MOVQ    BX, 24(SP)    0x00c8 00200 (/path/to/main.go:17) PCDATA  $0, $1    0x00c8 00200 (/path/to/main.go:17) CALL    "".MyStruct.incI3(SB)

看到指令把h1的成員i1,i2,i3,i4的值,傳給AX,CX,DX,BX,然後依次壓入堆棧,再call incI2;看到和incI2的差別是把h1的值全部通過棧傳入給了incI3,相當於複製了h1的內容,這就是傳值;即把h1做了一份拷貝,把拷貝的內容傳入incI3,而原來的h1保持不動。

看callee(incI3)的代碼

    0x0000 00000 (/path/to/main.go:8)  TEXT    "".MyStruct.incI3(SB), $16-32    0x0000 00000 (/path/to/main.go:8)  SUBQ    $16, SP    0x0004 00004 (/path/to/main.go:8)  MOVQ    BP, 8(SP)    0x0009 00009 (/path/to/main.go:8)  LEAQ    8(SP), BP    0x000e 00014 (/path/to/main.go:8)  FUNCDATA    $0, gclocals·ff19ed39bdde8a01a800918ac3ef0ec7(SB)    0x000e 00014 (/path/to/main.go:8)  FUNCDATA    $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)    0x000e 00014 (/path/to/main.go:8)  MOVQ    "".h+40(FP), AX    0x0013 00019 (/path/to/main.go:8)  MOVQ    AX, ""..autotmp_1(SP)    0x0017 00023 (/path/to/main.go:8)  INCQ    AX    0x001a 00026 (/path/to/main.go:8)  MOVQ    AX, "".h+40(FP)    0x001f 00031 (/path/to/main.go:8)  MOVQ    8(SP), BP    0x0024 00036 (/path/to/main.go:8)  ADDQ    $16, SP    0x0028 00040 (/path/to/main.go:8)  RET

對比incI2和incI3的實現代碼,可以清楚看到實際上receiver就是被作為一個參數來處理的,用值和用指標的差異,就和參數是傳值還是傳指標一樣的差異。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.