Methods

Promoted methods

Embedding T promotes its value-receiver methods to the method sets of S and *S; pointer-receiver methods are promoted to *S. Embedding *T can promote both to S and *S, when selectors are unambiguous. Example output: '1'.

Formal Syntax & Structure

go syntaxgo
type Outer struct { Inner }

Working Example

Example Snippetgo
package main
import "fmt"
type Counter struct{ N int }
func (c *Counter) Inc() { c.N++ }
type Outer struct{ *Counter }
func main() {  var p interface{ Inc() } = Outer{&Counter{}}  p.Inc()  fmt.Println(p.(Outer).N)}
Go Reference