Skip to content

Instantly share code, notes, and snippets.

@arxdsilva
Last active March 20, 2021 19:11
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 arxdsilva/441e6c3f3d6336f4b893b39f9cc286e6 to your computer and use it in GitHub Desktop.
Save arxdsilva/441e6c3f3d6336f4b893b39f9cc286e6 to your computer and use it in GitHub Desktop.
golang struct composition
package main
import (
"fmt"
)
type A struct {
Name string
Age string
}
func (a *A) SayMyName() {
fmt.Println("my name:", a.Name)
}
type B struct {
A
Name string
}
func (b *B) NewB() {
b.A = A{Name: "Arthur", Age: "19"}
b.Name = "Silva"
}
func (b *B) SayMyName() {
fmt.Println("my name:", b.Name)
}
func main() {
b := B{}
b.NewB()
fmt.Printf("%+v\n", b)
b.A.SayMyName()
b.SayMyName()
fmt.Printf("%+v\n", b.A)
}
// {A:{Name:Arthur Age:19} Name:Silva}
// my name: Arthur
// my name: Silva
// {Name:Arthur Age:19}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment