Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing interfaces in interfaces in Go

Tags:

interface

go

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{}
}
like image 368
Herks Avatar asked Oct 28 '12 10:10

Herks


People also ask

What is [] interface {} Golang?

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.

How do you declare an array of interfaces in Golang?

You can use interface{} array to build it. Then check the type when using the value. Save this answer.

CAN interface have variables in Go?

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 .


1 Answers

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

like image 195
Denys Séguret Avatar answered Oct 01 '22 08:10

Denys Séguret