How to Achieve Method Overriding Functionality in Go?

Patricia Arquette
Release: 2024-11-22 14:04:32
Original
502 people have browsed it

How to Achieve Method Overriding Functionality in Go?

Overriding Methods in Go

In Go, there is no direct equivalent to method overriding as found in object-oriented languages such as Java. However, there are alternative approaches to achieve similar functionality.

Consider the scenario where you have a base type Base with a method Get() that returns the string "base". You want to define a new type Sub that inherits from Base but has a different implementation of Get() that returns "Sub".

Your initial attempt of using composition like so:

type Sub struct {
    Base
}

func(sub *Sub) Get() string {
    return "Sub"
}
Copy after login

will not work because the Get() method of Sub cannot override the Get() method of Base. This is due to Go's method receiver semantics, where the receiver type (in this case *Base) determines the method to be called.

Using Interfaces

One alternative approach is to use interfaces. Interfaces define a contract of methods that a type must implement, similar to Java's abstract classes. In this case, you can define an interface Getter with a single Get() method.

type Getter interface {
    Get() string
}
Copy after login

You can then modify Base to implement the Getter interface:

type Base struct {
}

func (base *Base) Get() string {
    return "base"
}
Copy after login

Now, you can define Sub as a struct that embeds an instance of Base and also implements Getter:

type Sub struct {
    Base
}

func (sub *Sub) Get() string {
    return "Sub"
}
Copy after login

You can then use GetName() method to dynamically determine which implementation of Get() to call:

func (base *Base) GetName(getter Getter) string {
    if g, ok := getter.(Getter); ok {
        return g.Get()
    } else {
        return base.Get()
    }
}
Copy after login

In the main function, you can create an instance of Sub, cast it to Getter, and then pass it to GetName():

userType := Sub{}
fmt.Println(userType.GetName()) // prints "Sub"
Copy after login

Method Embedding

Another alternative is to use method embedding. This allows you to add a method to a struct by embedding a type that implements that method. In this case, you can define a new type SubGetter that embeds Base and implements a new Get() method:

type SubGetter struct {
    Base
}

func (sub *SubGetter) Get() string {
    return "Sub"
}
Copy after login

You can then use SubGetter as a method of Base:

type Base struct {
    SubGetter
}
Copy after login

This allows you to call the Get() method of SubGetter as if it were a method of Base:

base := Base{}
fmt.Println(base.Get()) // prints "Sub"
Copy after login

The above is the detailed content of How to Achieve Method Overriding Functionality in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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 Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template