Skip to content

Instantly share code, notes, and snippets.

@moehandi
Last active November 25, 2016 03:33
Show Gist options
  • Save moehandi/53f2014aa9f83f33ea81d8a63fc08b79 to your computer and use it in GitHub Desktop.
Save moehandi/53f2014aa9f83f33ea81d8a63fc08b79 to your computer and use it in GitHub Desktop.
Inheritance in golang with composition inside struct type
package main
import "fmt"
type Human struct {
name string
age int
address string
}
type Employee struct {
Human
company string
salary float64
}
type Student struct {
school string
money float64
}
func (h Human) printInfo() {
fmt.Printf("Hello, Im %s, I leave at %s and Im %v years old\n", h.name, h.address, h.age)
}
func (e Employee) printInfo() {
fmt.Printf("Hello, Im %s and Im Employee at %s with salary %v \n", e.name, e.company, e.salary)
}
func main() {
human := Human{"Mr.Huggo", 23, "Jakarta"}
human.printInfo()
employee := Employee{Human{"Moehandi", 25, "Bandung"}, "Golang.inc", 87000000.0}
employee.printInfo()
// TODO: Try add Student
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment