Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Created October 19, 2022 12:31
Show Gist options
  • Save mortymacs/516cb2f19197939f3d609eaccc1d1292 to your computer and use it in GitHub Desktop.
Save mortymacs/516cb2f19197939f3d609eaccc1d1292 to your computer and use it in GitHub Desktop.
Composition sample in Go
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