go語言介面和方法集問題筆記

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

注意: 此文章只是我的個人筆記,如有謬誤,錯誤, 請一定指出!


package main

import "fmt"

//IA test for methodset.
type IA interface {
    Value()
    Pointer()
}

//A is a  test type.
type A int

// Value receiver.
func (a A) Value() {

    fmt.Printf("Value:%p,  %d\n", &a, a)
}

/ /Pointer receiver.
func (a *A) Pointer() {

    fmt.Printf("Pointer:%p,  %d\n", a, *a)
}
func main() {

    // type A method set: (a A) Value()
    var a A = 1
    fmt.Printf("Origin: %p, %d\n", &a, a)
    fmt.Println("Value receiver------------------------------")
    a.Value()
    a.Pointer() //如: (&a). Value(),  a value converted to a Pointer  by go compiler, so value a can call type *A`s  pointer receiver method`

    // type *A method set: func (a A) Value(), func (a *A) Pointer()
    fmt.Println("Pointer receiver---------------------------")
    p := &a
    p.Value()
    p.Pointer()

    fmt.Println("interface call method by type *A-----------------------")
    var ia IA
    ia = &a
    ia.Value()
    ia.Pointer()

    fmt.Println("interface call method by type A-----------------------")
    //對於介面而言, 對於賦值給他的類型,會持有其副本, 即會複製, 且其持有的值不可取地址,會有更嚴格的類型檢查和限制, 不允許做如:A to *A的轉換 ,
    //所以當以類型A調用func (a *A) Pointer()時是不允許的, 因為類型A的方法集中不包括func (a *A) Pointer(), 也可以說, 類型A的方法集(method set)是類型*A的一個子集。
    //總結: 當以非介面類型調用pointer receiver method和 value receiver method時 go編譯器會協助做自動轉換,如: &a 或 *a ; 所以淡化value receiver和pointer receiver方法集的邊界。
    //但是介面不允許做這些自動轉換, 一就是一, 嚴格劃清方法集分界,這是golang介面的設計哲學,不是技術問題,
    //a interface must check type A or type *A , do not allow convert it between of them, so type A can not call type *A`s Pointer receiver method`
    ///ia = a //compliation error,coz type not match:  A does not implement IA (Pointer method has pointer receiver)
    //如果改成: ia = &a 就可以了, 因為:類型*A的方法集包括這個方法, 所以與介面匹配.
    //ia.Value()
    //ia.Pointer()

}

//program output:
//Origin: 0xc04200e290, 1 //注意: 與此地址不同的情況 , 皆產生了複製, 這就是value receiver與pointer receiver的區別,關注點.
//Value receiver------------------------------
//Value:0xc04200e2a8,  1
//Pointer:0xc04200e290,  1
//Pointer receiver---------------------------
//Value:0xc04200e2f0,  1
//Pointer:0xc04200e290,  1
//interface call method by type *A-----------------------
//Value:0xc04200e308,  1
//Pointer:0xc04200e290,  1

//interface call method by type A-----------------------




注意: 此文章只是我個人筆記, 如有錯漏,請一定指正, 共同學習, 我的郵箱: htyu_0203_39@sina.com



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.