Created
October 19, 2022 12:31
-
-
Save mortymacs/516cb2f19197939f3d609eaccc1d1292 to your computer and use it in GitHub Desktop.
Composition sample in Go
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 User struct { | |
Name string | |
Age int | |
} | |
func (u *User) ShowName() { | |
fmt.Println(u.Name) | |
} | |
type MyUser struct { | |
User | |
Score int | |
} | |
func (u *MyUser) ShowScore() { | |
fmt.Println(u.Name, u.Score) | |
} | |
func main() { | |
a := User{Name: "test"} | |
a.ShowName() | |
b := MyUser{User: a, Score: 10} | |
b.ShowScore() | |
b.ShowName() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment