Last active
July 4, 2022 17:06
-
-
Save ifindev/a193b086b858d9c362738d1e83a415f6 to your computer and use it in GitHub Desktop.
Struct Composition Go
This file contains 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
type Employee struct { | |
Name string | |
Age int | |
Height float32 // cm | |
Gender string | |
PositionName string | |
MonthsOfService int | |
Department string | |
} |
This file contains 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
type Employee struct { | |
Human Human | |
PositionName string | |
MonthsOfService int | |
Department string | |
} |
This file contains 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
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) | |
} |
This file contains 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
type Employee struct { | |
Human | |
PositionName string | |
MonthsOfService int | |
Department string | |
} |
This file contains 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
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() | |
} |
This file contains 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
// 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() | |
} |
This file contains 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
type Human struct { | |
Name string | |
Age int | |
Height float32 | |
Gender string | |
} |
This file contains 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
func NewHuman(name string, age int, height float32, gender string) *Human { | |
return &Human{ | |
Name: name, | |
Age: age, | |
Height: height, | |
Gender: gender, | |
} | |
} |
This file contains 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
func main() { | |
human := NewHuman("John Doe", 24, 180, "Male") | |
human.Introduction() | |
fmt.Println("") | |
human.SetName("Mariah Carey") | |
human.Introduction() | |
} |
This file contains 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
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