2013-07-06 118 views
2

我有下面的代碼塊:Golang參考列表

package main 

import (
    "fmt" 
    "container/list" 
) 

type Foo struct { 
    foo list //want a reference to the list implementation 
      //supplied by the language 
} 


func main() { 
    //empty 

}

當編譯我收到以下消息:

使用包列表的不選擇器

我的問題是,我該如何參考liststruct?或者,這不是圍繞結構的Go的適當成語。 (構圖)

回答

4

我可以看到兩個問題:導入fmt包不使用它

  1. 。在Go中未使用的導入導致編譯時錯誤;
  2. foo沒有正確聲明:list是一個包名稱不是類型;你想使用container/list包中的一個類型。

更正代碼:

package main 

import (
    "container/list" 
) 

type Foo struct { 
    // list.List represents a doubly linked list. 
    // The zero value for list.List is an empty list ready to use. 
    foo list.List 
} 

func main() {} 

您可以在Go Playground執行上面的代碼。
您還應該考慮閱讀container/list包的the official documentation

根據你想要做什麼,你可能也想知道Go允許你在結構或接口中嵌入類型。請閱讀Effective Go指南中的更多內容,並決定是否適合您的具體情況。

+0

確實。因爲它與錯誤無關,所以省略了我的主體。 – Woot4Moo