Skip to content

Instantly share code, notes, and snippets.

@ifindev
Last active July 4, 2022 17:06
Show Gist options
  • Save ifindev/a193b086b858d9c362738d1e83a415f6 to your computer and use it in GitHub Desktop.
Save ifindev/a193b086b858d9c362738d1e83a415f6 to your computer and use it in GitHub Desktop.
Struct Composition Go
type Employee struct {
Name string
Age int
Height float32 // cm
Gender string
PositionName string
MonthsOfService int
Department string
}
type Employee struct {
Human Human
PositionName string
MonthsOfService int
Department string
}
func (e *Employee) Introduction() {
fmt.Printf("Hello my name is %s. My age is %d. Nice to meet you :)\n", e.Name, e.Age)
fmt.Printf("I have been working on the department of %s at company X as a %s for almost %d years now", e.Department, e.PositionName, e.MonthsOfService)
}
type Employee struct {
Human
PositionName string
MonthsOfService int
Department string
}
func NewEmployee(
person Human,
positionName string,
monthsOfService int,
department string,
) *Employee {
return &Employee{
Human: person,
PositionName: positionName,
MonthsOfService: monthsOfService,
Department: department,
}
}
func main() {
person := NewHuman("John Doe", 24, 180, "Male")
employee := NewEmployee(*person, "Sr. Software Engineer", 2, "Core Banking Integration")
employee.Introduction()
employee.SetName("Mariah Carey")
employee.Introduction()
}
// You can run this code on https://go.dev/play/
import "fmt"
type Human struct {
Name string
Age int
Height float32 // cm
Gender string
}
func NewHuman(name string, age int, height float32, gender string) *Human {
return &Human{
Name: name,
Age: age,
Height: height,
Gender: gender,
}
}
func (h *Human) SetName(name string) {
h.Name = name
}
func (h *Human) Introduction() {
fmt.Printf("Hello my name is %s. My age is %d. Nice to meet you :)\n", h.Name, h.Age)
}
type Employee struct {
Human
PositionName string
MonthsOfService int
Department string
}
func NewEmployee(
person Human,
positionName string,
monthsOfService int,
department string,
) *Employee {
return &Employee{
Human: person,
PositionName: positionName,
MonthsOfService: monthsOfService,
Department: department,
}
}
func (e *Employee) Introduction() {
fmt.Printf("Hello my name is %s. My age is %d. Nice to meet you :)\n", e.Name, e.Age)
fmt.Printf("I have been working on the department of %s at company X as a %s for almost %d years now", e.Department, e.PositionName, e.MonthsOfService)
}
func main() {
human := NewHuman("John Doe", 24, 180, "Male")
human.Introduction()
human.SetName("Mariah Carey")
human.Introduction()
fmt.Println("\n================================================================\n")
person := NewHuman("John Doe", 24, 180, "Male")
employee := NewEmployee(*person, "Sr. Software Engineer", 2, "Core Banking Integration")
employee.Introduction()
}
type Human struct {
Name string
Age int
Height float32
Gender string
}
func NewHuman(name string, age int, height float32, gender string) *Human {
return &Human{
Name: name,
Age: age,
Height: height,
Gender: gender,
}
}
func main() {
human := NewHuman("John Doe", 24, 180, "Male")
human.Introduction()
fmt.Println("")
human.SetName("Mariah Carey")
human.Introduction()
}
func (h *Human) SetName(name string) {
h.Name = name
}
func (h *Human) Introduction() {
fmt.Printf("Hello my name is %s. My age is %d. Nice to meet you :)", h.Name, h.Age)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment