go語言學習道路(插曲)-一個疑惑的問題

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。最近寫go語言程式的時候,發現了一個問題。先看代碼:
// MusicPlay project main.gopackage mainimport ("fmt")type Cat interface {Meow()}type Tabby struct{}func (*Tabby) Meow() { fmt.Println("meow") }func GetACat() Cat {var myTabby *Tabby = nil// Oops, we forgot to set myTabby to a real valuereturn myTabby}func TestGetACat() {var ICat Catvar tabby *Tabby = nilICat = tabby//ICat = GetACat()//ICat = nilif ICat == nil {fmt.Println("GetTest Failed!")}}func main() {TestGetACat()}


在調用TestGetACat()函數中,我的想法是ICat應該為nil,然後調用fmt.Println("GetTest Failed!")。運行結果卻是沒有調用,這個頓時我就感覺有點迷惑了,然後直接加上ICat = nil卻又是可以的,於是乎百度了一下。發現了一遍文章,地址:http://geek.csdn.net/news/detail/102187。

他的理解是這樣的: ICat指向一個Tabby指標對象,該指標指向nil。所以ICat不為nil。

暫時只能夠這麼理解,記錄下來




聯繫我們

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