No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Creando la Interfaz Animal

21/42
Recursos

Aportes 14

Preguntas 2

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

Interesante el concepto de polimorfismo en Go con las interfaces.

package main

import "fmt"

type animal interface {
	makeSound() string
}

type dog struct{}

type cat struct{}

func (d dog) makeSound() string {
	return "Waow waow"
}

func (c cat) makeSound() string {
	return "Miau"
}

func animalMakeSound(a animal) {
	fmt.Println(a.makeSound())
}

func main() {
	cafu := dog{}
	sisa := cat{}

	animalMakeSound(cafu)
	animalMakeSound(sisa)

}

esa implementacion implicita esta cool!., no sabia que se podia hacer eso con las interfaces de ese modo.

Para los amantes de Avatar cómo yo:

package main

import "fmt"

type maestro interface {
	presentacion() string
}

type agua struct {
	nombre string
}

type tierra struct {
	nombre string
}

type fuego struct {
	nombre string
}

type aire struct {
	nombre string
}

func (a agua) presentacion() string {
	returnString := "Mi nombre es " + a.nombre + " y soy un maestro agua."
	return returnString
}

func (t tierra) presentacion() string {
	returnString := "Mi nombre es " + t.nombre + " y soy un maestro tierra."
	return returnString
}

func (f fuego) presentacion() string {
	returnString := "Mi nombre es " + f.nombre + " y soy un maestro fuego."
	return returnString
}

func (ai aire) presentacion() string {
	returnString := "Mi nombre es " + ai.nombre + " y soy un maestro aire."
	return returnString
}

func presentarMaestro(m maestro) {
	fmt.Println(m.presentacion())
}

func main() {
	a := agua{
		nombre: "Katara",
	}
	t := tierra{
		nombre: "Toph",
	}
	f := fuego{
		nombre: "Zuko",
	}
	aa := aire{
		nombre: "Aang",
	}

	presentarMaestro(a)
	presentarMaestro(t)
	presentarMaestro(f)
	presentarMaestro(aa)
}

Nombres de interfaz

Por convención, las interfaces de un método se nombran por el nombre del método más un sufijo -er o una modificación similar para construir un nombre de agente: Reader, Writer, Formatter, CloseNotifier etc.

Hay varios de esos nombres y es productivo honrarlos y los nombres de función que capturan. Read, Write, Close, Flush, String, etc. tienen firmas y significados canónicos. Para evitar confusiones, no le dé a su método uno de esos nombres a menos que tenga la misma firma y significado. Por el contrario, si su tipo implementa un método con el mismo significado que un método en un tipo conocido, asígnele el mismo nombre y firma; llame a su método de string-converter String not ToString.

https://go.dev/doc/effective_go#interface-names

un poco tosco el hecho de las intefaces no se implementen de manera explicita, puede llevar a varias confusiones , existe alguna forma de hacerlo explicito ??

package main

import "fmt"

type animal interface {
	move() string
}

type dog struct {

}

type fish struct {

}

type bird struct {

}

func (dog) move() string {
	return "I'm dog and i walk"
}

func (fish) move() string {
	return "I'm fish and i'm swimming"
}

func (bird) move() string {
	return "I'm bird and i'm fling"
}

func moveAnimal(a animal) {
	fmt.Println(a.move())
}

func main() {
	d := dog{}
	moveAnimal(d)
	f := fish{}
	moveAnimal(f)
	b := bird{}
	moveAnimal(b)
}
package main

import "fmt"

type animal interface {
	mover() string
}

type perro struct{}
type ave struct{}
type pez struct{}

func (perro) mover() string {
	return "soy un perro y estoy caminando"
}
func (ave) mover() string {
	return "soy un ave y estoy volando"
}

func (pez) mover() string {
	return "soy un pez y estoy nadando"
}

func moverAnimal(a animal) {
	fmt.Println(a.mover())
}

func main() {
	p := perro{}
	moverAnimal(p)
	a := ave{}
	moverAnimal(a)
	pe := pez{}
	moverAnimal(pe)
}

package main

import "fmt"

type animal interface {
	mover() string
}

type perro struct {
}

type pez struct {
}

type pajaro struct {
}

func (perro) mover() string {
	return "Soy un perro y camino"
}

func (pez) mover() string {
	return "Soy un pez y nado"
}

func (pajaro) mover() string {
	return "Soy un pajaro y estoy nadando"
}


func moverAnimal(a animal) {
	fmt.Println(a.mover())
}

func main() {
	p := perro{}
	pe := pez{}
	pa := pajaro{}

	moverAnimal(p)
	moverAnimal(pe)
	moverAnimal(pa)
	
}

genial 😃 estoy atento cuando hable de los package… para de alguna manera ordenar lo struct en distintos paquetes 😃

Añadí a la implementación, un slice de animales, así con un for range podemos usar la función una sola vez y simplificar más el código


package main

import "fmt"

type animal interface {
	move() string
}

type dog struct {
}

type fish struct {
}

type bird struct {
}

// func (d dog) walk() string {
// 	return "I am a dog and walk"
// }

// func (f fish) swim() string {
// 	return "I am a fish and swim"
// }

// func (b bird) fly() string {
// 	return "I am a bird and fly"
// }

func (d dog) move() string {
	return "I am a dog and walk"
}

func (f fish) move() string {
	return "I am a fish and swim"
}

func (b bird) move() string {
	return "I am a bird and fly"
}

func move(a animal) {
	fmt.Println(a.move())
}

// func moveDog(d dog) {
// 	fmt.Println(d.walk())
// }

// func moveFish(f fish) {
// 	fmt.Println(f.swim())
// }

// func moveBird(b bird) {
// 	fmt.Println(b.fly())
// }

func main() {
	d := dog{}
	// moveDog(d)
	move(d)
	f := fish{}
	// moveFish(f)
	move(f)
	b := bird{}
	// moveBird(b)
	move(b)

	animals := []animal{
		d,
		f,
		b,
	}
	fmt.Println(animals)
	for _, animal := range animals {
		move(animal)
	}
}
package main

import "fmt"

type animal interface {
	move() string
}

type animalType struct {
	name string
	action string
}
func (at animalType) move() string {
	return "I'm a " + at.name + " and i " + at.action
}

func moveAnimal(a animal) {
	fmt.Println(a.move())
}

func main() {
	d := animalType{
		name: "dog",
		action: "walk",
	}
	moveAnimal(d)
	f := animalType{
		name: "fish",
		action: "swim",
	}
	moveAnimal(f)
	b := animalType{
		name: "bird",
		action: "fly",
	}
	moveAnimal(b)
}
ackage main

import (
	"fmt"
)

type animal interface {
	raza() string
}

type perro struct{}
type gato struct{}
type caballo struct{}

func (p perro) raza() string {
	return "Pitbull"
}

func (c caballo) raza() string {
	return "Mustang"
}

func (g gato) raza() string {
	return "Savannah"
}

func razaAnimal(a animal) {
	fmt.Println(a.raza())
}

func main() {
	vandalo := perro{}
	michin := caballo{}
	trueno := gato{}

	razaAnimal(vandalo)
	razaAnimal(michin)
	razaAnimal(trueno)
	
}
package main

import (
	"fmt"
)

type animal interface {
	raza() string
}

type perro struct{}
type gato struct{}
type caballo struct{}

func (p perro) raza() string {
	return "Pitbull"
}

func (c caballo) raza() string {
	return "Mustang"
}

func (g gato) raza() string {
	return "Savannah"
}

func razaAnimal(a animal) {
	fmt.Println(a.raza())
}

func main() {
	vandalo := perro{}
	michin := caballo{}
	trueno := gato{}

	razaAnimal(vandalo)
	razaAnimal(michin)
	razaAnimal(trueno)
	
}```