I don't understand the following code snippet in the container/heap
package.
type Interface interface {
sort.Interface //Is this line a method?
Push(x interface{})
Pop() interface{}
}
interface{} means you can put value of any type, including your own custom type. All types in Go satisfy an empty interface ( interface{} is an empty interface). In your example, Msg field can have value of any type.
You can use interface{} array to build it. Then check the type when using the value. Save this answer.
Since the interface is a type just like a struct, we can create a variable of its type. In the above case, we can create a variable s of type interface Shape .
This is a type declaration.
The heap.Interface
interface embeds the sort.Interface
interface.
You can see it as a kind of inheritance/specialization : it means that the structs implementing the heap.Interface
interface are defined as the ones that implement the sort.Interface
methods and the Push
and Pop
methods.
Interface embeding is described in Effective Go : http://golang.org/doc/effective_go.html#embedding
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With