Marty Wallace
Marty Wallace

Reputation: 35734

Go - Interface containing slice wont compile

type MyType interface {
    MyStringSlice []string
}

What is wrong with this?

It works well as a struct as in:

type MyType struct {
    MyStringSlice []string
}

but compiles with the following error when set as interface:

syntax error: unexpected [, expecting (

Upvotes: 2

Views: 84

Answers (1)

seong
seong

Reputation: 1996

An interface can only hold methods or other interfaces. Have a look at the Language Specification.

Upvotes: 4

Related Questions