|
| 1 | +package main |
| 2 | + |
| 3 | +import "fmt" |
| 4 | + |
| 5 | +//Go no cuenta con una palabra para declarar herencia en los structs, sin embargo sí tiene algo parecido. |
| 6 | +//Para que un struct en go posea todos los campos que declara otro struct, le pasamos este último como un campo anónimo |
| 7 | + |
| 8 | +type Animal struct { |
| 9 | + code string |
| 10 | + name string |
| 11 | +} |
| 12 | + |
| 13 | +type Cat struct { |
| 14 | + Animal |
| 15 | +} |
| 16 | + |
| 17 | +type Dog struct { |
| 18 | + Animal |
| 19 | +} |
| 20 | + |
| 21 | +type Sound interface { |
| 22 | + MakeSound() |
| 23 | +} |
| 24 | + |
| 25 | +func (c *Cat) MakeSound() { |
| 26 | + fmt.Printf("El gato %s hace miau miau \n", c.name) |
| 27 | +} |
| 28 | + |
| 29 | +func (d *Dog) MakeSound() { |
| 30 | + fmt.Printf("El perro %s hace wau wau \n", d.name) |
| 31 | +} |
| 32 | + |
| 33 | +//Está función puede recibir cualquier struct que implemente la interfaz Sound |
| 34 | +func PrintAnimal(sound Sound) { |
| 35 | + sound.MakeSound() |
| 36 | +} |
| 37 | + |
| 38 | +//============================================================= |
| 39 | +//======================= EXTRA =============================== |
| 40 | +//============================================================= |
| 41 | +type EmployeeManager interface { |
| 42 | + AddEmployee(employee Employee) |
| 43 | + PrintEmployees() |
| 44 | +} |
| 45 | + |
| 46 | +type Employee struct { |
| 47 | + id int |
| 48 | + name string |
| 49 | + employees []Employee |
| 50 | +} |
| 51 | + |
| 52 | +func (e *Employee) AddEmployee(employee Employee) { |
| 53 | + e.employees = append(e.employees, employee) |
| 54 | +} |
| 55 | + |
| 56 | +func (e *Employee) PrintEmployees() { |
| 57 | + for _, employee := range e.employees { |
| 58 | + fmt.Printf("id : %d, name:%s\n", employee.id, employee.name) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +type Manager struct { |
| 63 | + Employee |
| 64 | +} |
| 65 | + |
| 66 | +func (m *Manager) CoordinateProjects() { |
| 67 | + fmt.Printf("%s está coordinando todos los proyectos de la empresa.\n", m.name) |
| 68 | +} |
| 69 | + |
| 70 | +type ProjectManager struct { |
| 71 | + Employee |
| 72 | + project string |
| 73 | +} |
| 74 | + |
| 75 | +func (pm *ProjectManager) CoordinateProject() { |
| 76 | + fmt.Printf("%s está coordinando su proyecto %s \n", pm.name, pm.project) |
| 77 | +} |
| 78 | + |
| 79 | +type Programmer struct { |
| 80 | + Employee |
| 81 | + languages []string |
| 82 | +} |
| 83 | + |
| 84 | +func (e *Programmer) AddEmployee(employee Employee) { |
| 85 | + fmt.Println("Un programador no puede tener empleados, no se ha agregado") |
| 86 | +} |
| 87 | + |
| 88 | +func (e *Programmer) Code() { |
| 89 | + fmt.Printf("%s está programando en %v", e.name, e.languages) |
| 90 | +} |
| 91 | + |
| 92 | +func main() { |
| 93 | + cat1 := Cat{Animal{code: "0001", name: "Thor"}} |
| 94 | + PrintAnimal(&cat1) |
| 95 | + dog1 := Dog{Animal{code: "0002", name: "Run Run"}} |
| 96 | + PrintAnimal(&dog1) |
| 97 | + //EXTRA |
| 98 | + m := Manager{Employee{id: 100, name: "Amador"}} |
| 99 | + m.AddEmployee(Employee{id: 200, name: "Alex"}) |
| 100 | + p := Programmer{languages: []string{"Java", "Go"}, Employee: Employee{id: 100, name: "Mia"}} |
| 101 | + p.AddEmployee(Employee{id: 300, name: "Pedro"}) |
| 102 | + |
| 103 | + var employeeManagers []EmployeeManager |
| 104 | + employeeManagers = append(employeeManagers, &m, &p) |
| 105 | + |
| 106 | + for _, manager := range employeeManagers { |
| 107 | + manager.AddEmployee(Employee{id: 500, name: "New Employee"}) |
| 108 | + } |
| 109 | + m.CoordinateProjects() |
| 110 | + m.PrintEmployees() |
| 111 | +} |
0 commit comments