Parsing polymorphism in Go - no interfaces required

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

The following column will introduce you to polymorphism in Parsing polymorphism in Go - no interfaces required from the golang tutorial column - 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

dog and duck have the same type ( *Animal). Each variable is instantiated using a different constructor, and they have different behaviors when makeNoisethe 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

The Animal structure contains name and legs attributes, as well as a mkeNoiseFn attribute. This attribute It is actually a function that accepts a *Animal parameter and returns a string. The

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

makeNoise method is really just a wrapper that calls the corresponding animal makenoiseFn and 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 its makeNoiseFn properties. Now, the makeNoise method 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 you can do, not what should be 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template