Struct 自定义结构中的Golang引用列表
我有以下代码块:Struct 自定义结构中的Golang引用列表,struct,go,composition,Struct,Go,Composition,我有以下代码块: 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 } 编译时,我收到以下消息: 不在选择器中使用包列表 我的问题
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
}
编译时,我收到以下消息:
不在选择器中使用包列表
我的问题是,如何在结构中引用列表?或者这不是Go for wrapping结构中的正确习惯用法。作文我发现两个问题: 导入fmt包而不使用它。未使用的导入导致编译时错误; foo声明不正确: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允许您在结构或接口中嵌入类型。阅读指南中的更多内容,并确定这对您的特定情况是否有意义。我发现两个问题: 导入fmt包而不使用它。未使用的导入导致编译时错误; foo声明不正确: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允许您在结构或接口中嵌入类型。阅读指南中的更多内容,并确定这对您的特定情况是否有意义