Skip to content

Instantly share code, notes, and snippets.

@tiagox
Last active March 17, 2023 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiagox/89671572dbdb85e6489fa959b4097882 to your computer and use it in GitHub Desktop.
Save tiagox/89671572dbdb85e6489fa959b4097882 to your computer and use it in GitHub Desktop.
Interfaces en Go
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
}
$ 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