嵌入式字段是否计入接口

In Golang, I can have an embedded fields inside of a struct. The embedded field gets "promoted", and the new struct gets to use all the functions of the embedded fields as if it's part of itself. So my question is, does the embedded fields' functions count towards interface implementation? For example:

type Foo struct {
    Name string
}

func (f *Foo) Name() {
    fmt.Println(f.Name)
}

type Hello interface {
    Name()
    Hello()
}

type Bar struct {
    World string
    *Foo
}

func (b *Bar) Hello() {
    fmt.Println("Hello")
}

In the code above, Bar{} does not implement a function named Name(), but Foo{} does. Since Foo{} is an embedded field inside of Bar{}, is Bar{} a Hello type?

Here's how to trace this down in the language specification

Find out what it means to implement an interface in the "Interface types" section:

Interface types ¶

An interface type specifies a method set called its interface. A variable of interface type can store a value of any type with a method set that is any superset of the interface. Such a type is said to implement the interface.

Or from the section on "Method sets"

The method set of a type determines the interfaces that the type implements...

So what is important is the method set of the type you want to implement the interface. Let's see what constitutes a method set in the case of embedded fields:

The first place I checked was the section "Method Sets", but it sends us elsewhere:

Further rules apply to structs containing embedded fields, as described in the section on struct types.

So we go to the section "Struct types" and find:

A field or method f of an embedded field in a struct x is called promoted if x.f is a legal selector that denotes that field or method f.

...

Given a struct type S and a defined type T, promoted methods are included in the method set of the struct as follows:

  • If S contains an embedded field T, the method sets of S and *S both include promoted methods with receiver T. The method set of *S also includes promoted methods with receiver *T.
  • If S contains an embedded field *T, the method sets of S and *S both include promoted methods with receiver T or *T.

So, given that the methods of the embedded field are promoted, they are included in the method set of the containing struct. As we saw above the method set of any type is what determines whether it implements an interface.