Last active
March 20, 2021 19:11
-
-
Save arxdsilva/441e6c3f3d6336f4b893b39f9cc286e6 to your computer and use it in GitHub Desktop.
golang struct composition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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