Parsing polymorphism in Go - no interfaces required

藏色散人
Release: 2020-12-10 13:50:07
forward
1792 people have browsed it

The following column will introduce you to polymorphism in Parsing polymorphism in Go - no interfaces required from thegolang tutorialcolumn - no interfaces are required, I hope it will be helpful to friends in need!

Parsing polymorphism in Go - no interfaces required

What if you want to use polymorphism in Parsing polymorphism in Go - no interfaces required, but don't like interfaces? Read on...

First, let's see what we want to do:

var dog, duck *Animal dog = NewDog("fido") duck = NewDuck("donald") fmt.Println(dog.makeNoise()) // fido says woof! fmt.Println(duck.makeNoise()) // donald says quack!
Copy after login

dogandduckhave the same type (*Animal). Each variable is instantiated using a different constructor, and they have different behaviors whenmakeNoisethe same method is called.

Usually, this example is the purpose for us to use the interface, but we don't want the actual use to be so simple.

Let’s see how to make this work:

Please click here (https://play.golang.org/p/P5Ovn_K-yyo) to view the complete code

type Animal struct { makeNoiseFn func(*Animal) string name string legs int }
Copy after login

TheAnimalstructure containsnameandlegsattributes, as well as amkeNoiseFnattribute. This attribute It is actually a function that accepts a*Animalparameter and returns a string. The

func (a *Animal) makeNoise() string { return a.makeNoiseFn(a) }
Copy after login

makeNoisemethod is really just a wrapper that calls the corresponding animalmakenoiseFnand takes as its argument a pointer to the animal itself.

func NewDog(name string) *Animal { return &Animal{ makeNoiseFn: func(a *Animal) string { return a.name + " says woof!" }, legs: 4, name: name, } } func NewDuck(name string) *Animal { return &Animal{ makeNoiseFn: func(a *Animal) string { return a.name + " says quack!" }, legs: 4, name: name, } }
Copy after login

Now, all we have to do is make the same type behave differently and assign different functions to itsmakeNoiseFnproperties. Now, themakeNoisemethod calls the corresponding function depending on whether the animal is a dog or a duck.

Should I do this?

NO!

This article is intended to show you what youcando, not whatshouldbe done . If you need to implement polymorphism, interfaces are a better approach. If using an interface, this code would look like this:

type Animal interface { makeNoise() string } type Dog struct { name string legs int } func (d *Dog) makeNoise() string { return d.name + " says woof!" } type Duck struct { name string legs int } func (d *Duck) makeNoise() string { return d.name + " says quack!" } func NewDog(name string) Animal { return &Dog{ legs: 4, name: name, } } func NewDuck(name string) Animal { return &Duck{ legs: 4, name: name, } } func main() { var dog, duck Animal dog = NewDog("fido") duck = NewDuck("donald") fmt.Println(dog.makeNoise()) // fido says woof! fmt.Println(duck.makeNoise()) // donald says quack! }
Copy after login

Original address: https://www.sohamkamani.com/golang/2019-03-29-polymorphism-without-interfaces/

Translation address: https://learnku.com/go/t/52404

The above is the detailed content of Parsing polymorphism in Go - no interfaces required. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!