2

I have seen interfaces defined like:

type MyInterface interface{
   Method()
}

and

type MyInterface []interface{
   Method()
}

What is the difference in these two definitions?

1
  • 3
    The first is an interface type, the second is a slice type whose element type is interfacs. Like type Ints []int is a slice of ints, type Ifaces []interface{... is a slice of interfaces. Commented Feb 23, 2019 at 8:55

3 Answers 3

1

The first is defining an interface with a method of Method(), while the second is defining the a slice of interface. The statement utilizes the form of type literal and can be interepted as:

type MyInterface [](interface{
   Method()
})

Where interface{...} here is a type literal. Which has the same effect of

type I = interface{
   Method()
}
type MyInterface []I

See more on type literal: https://golang.org/ref/spec#Types

Note: MyInterface would be a very bad name in the second one.

Sign up to request clarification or add additional context in comments.

Comments

0

Basically when you create

type Iface interface {
    Age() uint
    Name() string
}

type SliceIface []interface {
    Age() uint
    Name() string
}

// then SliceIface and []Iface has the same behaviour

so you could say

type SliceIface []Iface

Have a look at following code

https://play.golang.org/p/DaujSDZ8p-N

2 Comments

Interestingly though, if you exchange SliceIface with []Iface on line 66, it won't compile.
Yeah, I meant it behaves like []Iface. By the functional site, it is the same. But you cannot say to compiler that []Iface and SliceIface are the same, because both are declared as independent interfaces.
0

The first one is an interface declaration with a method method() declared in it.

type MyInterface interface{
   Method()
}

The second is that a variable with type []interface{} is not an interface! It is a slice whose element type happens to be interface{}

type MyInterface []interface{
   Method()
}

Plus, a variable with type []interface{} has a specific memory layout, known at compile time.

2 Comments

Any link to more details on memory layout? I'd be interested to check more details on that. Thanks.
Each interface{} takes up two words (one word for the type of what is contained, the other word for either the contained data or a pointer to it). As a consequence, a slice with length N and with type []interface{} is backed by a chunk of data that is N*2 words long. This is different than the chunk of data backing a slice with type []MyType and the same length. Its chunk of data will be N*sizeof(MyType) words long. The result is that you cannot quickly assign something of type []MyType to something of type []interface{}; the data behind them just look different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.