简体   繁体   中英

Go error: "embedded type cannot be a pointer to interface"

The following code gives compile-time error:

type IFile interface {
    Read() (n int, err error)
    Write() (n int, err error)
}

type TestFile struct {
   *IFile
}

Error:

./test.go:18: embedded type cannot be a pointer to interface

Why can't I embed *IFile ?

The language spec does not allow it. Relevant section from the spec: Struct types:

A field declared with a type but no explicit field name is an anonymous field, also called an embedded field or an embedding of the type in the struct. An embedded type must be specified as a type name T or as a pointer to a non-interface type name *T , and T itself may not be a pointer type. The unqualified type name acts as the field name.

A pointer to an interface type is very rarely useful, as an interface type may hold a pointer or a non-pointer value.

Having said that, if a concrete type that implements your IFile type is a pointer type, then a pointer value will be wrapped in an interface value of type IFile , so you still have to embed IFile , just the value that implements IFile will be a pointer value, eg

// Let's say *FileImpl implements IFile:
f := TestFile{IFile: &FileImpl{}}

Edit: Answer to your comment:

First, this is Go, not C++. In Go, interfaces are not pointers, but they are represented as a pair of (type;value) , where "value" may be pointer or non-pointer. More about this in blog post: The Laws of Reflection: The representation of an interface .

Second, if FileImpl is a type, f := TestFile{IFile : &FileIml} is obviously a compile-time error, you need a value of *FileImpl and &FileImpl clearly isn't. You need for example a composite literal which is in the form of &FileImpl{} , so it should be as I posted above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM