I have the following block of code:
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
}
When compiling I receive the following message:
use of package list not in selector
My question is, how do I reference list
within a struct
? Or is this not the proper idiom in Go for wrapping structures. (Composition)
I can see two problems:
fmt
package without using it. In Go unused imports result in compile-time errors;foo
is not declared correctly: list
is a package name not a type; you want to use a type from the container/list
package.Corrected code:
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() {}
You can execute the above code in the Go Playground.
You should also consider reading the official documentation of the container/list
package.
Depending on what you're trying to do, you might also want to know that Go allows you to embed types within a struct or interface. Read more in the Effective Go guide and decide wether or not this makes sense for your particular case.