Last active
March 17, 2023 00:14
-
-
Save tiagox/89671572dbdb85e6489fa959b4097882 to your computer and use it in GitHub Desktop.
Interfaces en 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
package main | |
import "fmt" | |
type Humano interface { | |
Vivir() | |
} | |
type Alumno struct { | |
Legajo string | |
Nombre string | |
} | |
func (a Alumno) Vivir() { | |
fmt.Println("Me llamo", a.Nombre, "y estoy viviendo") | |
} | |
type Abogado struct { | |
Matricula string | |
Nombre string | |
} | |
func hacer(humano Humano) { | |
humano.Vivir() | |
} | |
func main() { | |
alumno := Alumno{Legajo: "1234", Nombre: "Juan"} | |
abogado := Abogado{Matricula: "5678", Nombre: "Ana"} | |
hacer(alumno) | |
hacer(abogado) // Error: Abogado no cumple con la interfaz de Humano | |
} |
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
$ go run main.go | |
# command-line-arguments | |
./main.go:32:8: cannot use abogado (variable of type Abogado) as Humano value in argument to hacer: Abogado does not implement Humano (missing method Vivir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment