Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Last active April 1, 2021 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/036fe248d39acaa295dfd77f8e3b8580 to your computer and use it in GitHub Desktop.
Save xeoncross/036fe248d39acaa295dfd77f8e3b8580 to your computer and use it in GitHub Desktop.
Example of using struct enbeding to implement an interface: https://play.golang.org/p/9RAY8RI7_N7
package main
import (
"fmt"
)
type A struct {
B string
}
func (a *A) Value() string {
return a.B
}
type C struct {
*A
}
// C does not implement the interface, but A does and C contains it
//func (c *C) Value() string {
// return c.A.Value()
//}
type Z interface {
Value() string
}
var _ Z = (*C)(nil) // The compiler checks that C implements Z (via A) here
func main() {
a := &A{"hello"}
c := &C{a}
fmt.Println(c.Value())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment