Lädt...


🔧 Inheritance in Go


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Go is not an object oriented language. However, you could still get the benefit of OOP by using different approach provided by Go language. This article talks about how you can "inherit" behavior from one type to another in Go.

Embedding

In Go, to automatically implement the methods and properties of one type to another is called "embedding". Look at the following code:

type Greeter struct {
    Style string
}

func (g *Greeter) Greet(name string) string {
    return fmt.Sprintf("%s, %s", g.Style, name)
}

type CustomerService struct {
    Greeter // embed Greeter in CustomerService
}

On the code above, type CustomerService "embed" type Greeter into it. By doing that, instance of CustomerService automatically has method Greet defined on Greeter.

c := CustomerService{}
c.Style = "Halo"
greeting := c.Greet("John") // "Halo, John"

Embedding multiple types

In OOP, you are allowed to inherit a class from only one parent. In go, you can embed multiple types.

type Greeter struct {
    Style string
}

func (g *Greeter) Greet(name string) string {
    return fmt.Sprintf("%s, %s", g.Style, name)
}

type Helper struct {}

func (h *Helper) Help() string {
    return "I'm here to help"
}

type CustomerService struct {
    Greeter
    Helper
}

However, you have to make sure there is no overlapping methods between the types that you embed. Otherwise, Go will gives you "ambigous selector" error and won't compile.

type Greeter struct {}

func (g *Greeter) Greet(name string) string {
    return fmt.Sprintf("%s, %s", g.Style, name)
}

type Helper struct {}

// second Greet method is defined in Helper.
// type that embed both Greeter and Helper
// wouldn't be able to call `Greet`
func (h *Helper) Greet(name string) string {
    return fmt.Sprintf("Hello, %s", name)
}

type CustomerService struct {
    Greeter
    Helper
}

func main() {
    c := CustomerService{}
    fmt.Println(c.Greet("John")) // ambiguous selector c.Greet
}

Conclusion

While go is not an OOP language, you still can get the benefit of inheritance using the "embedding" technique.

...

🔧 What is Inheritance in OOP in Python? What are the Different Types of Inheritance


📈 35.51 Punkte
🔧 Programmierung

🔧 Understanding Prototypes in JavaScript: The Backbone of Inheritance


📈 17.76 Punkte
🔧 Programmierung

🔧 Introduction to Inheritance in Object-Oriented Programming


📈 17.76 Punkte
🔧 Programmierung

🕵️ Low CVE-2020-2198: Jenkins Project inheritance


📈 17.76 Punkte
🕵️ Sicherheitslücken

🎥 CSS highlight inheritance is changing


📈 17.76 Punkte
🎥 Video | Youtube

🔧 PostgreSQL Role Inheritance in Reverse: Discovering Descendant Roles in Reverse Gear


📈 17.76 Punkte
🔧 Programmierung

🔧 Composition vs Inheritance in React 🔻


📈 17.76 Punkte
🔧 Programmierung

🔧 Combining Inheritance and Polymorphism in Real-World Applications


📈 17.76 Punkte
🔧 Programmierung

🕵️ Low CVE-2020-2197: Jenkins Project inheritance


📈 17.76 Punkte
🕵️ Sicherheitslücken

🔧 New in Chrome 131: external CSS highlight inheritance, improvements to details, and more!


📈 17.76 Punkte
🔧 Programmierung

🔧 Composition or Inheritance: What’s better?


📈 17.76 Punkte
🔧 Programmierung

🔧 C# Tip: Records with Inheritance


📈 17.76 Punkte
🔧 Programmierung

🔧 What is Prototypal Inheritance in JavaScript? Explained with Code Examples


📈 17.76 Punkte
🔧 Programmierung

🎥 Mixins (Multiple inheritance) | More Python for Beginners [10 of 20]


📈 17.76 Punkte
🎥 Video | Youtube

🔧 Understanding JavaScript's Prototypal Inheritance - A Dev's Guide


📈 17.76 Punkte
🔧 Programmierung

🔧 Mastering Java: Classes, Methods, Inheritance, and Encapsulation


📈 17.76 Punkte
🔧 Programmierung

🪟 How to Block Group Policy Inheritance For a Domain/OU


📈 17.76 Punkte
🪟 Windows Tipps

🔧 Understanding the final Keyword in PHP: Preventing Inheritance and Overriding


📈 17.76 Punkte
🔧 Programmierung

🔧 Inheritance and Composition in C#


📈 17.76 Punkte
🔧 Programmierung

🎥 Inheritance | More Python for Beginners [8 of 20]


📈 17.76 Punkte
🎥 Video | Youtube

🔧 Understanding JavaScript's Prototypal Inheritance - A Dev's Guide


📈 17.76 Punkte
🔧 Programmierung

🔧 PHP OOP Concepts: Classes, Objects and Inheritance


📈 17.76 Punkte
🔧 Programmierung

🔧 PostgreSQL Role Inheritance at a Glance


📈 17.76 Punkte
🔧 Programmierung

🔧 Day 12: Inheritance


📈 17.76 Punkte
🔧 Programmierung

🎥 Demo: Inheritance | More Python for Beginners [9 of 20]


📈 17.76 Punkte
🎥 Video | Youtube

🔧 Solving Inheritance Compatibility Issues Between LayerZero's NonblockingLzApp and OpenZeppelin's Ownable


📈 17.76 Punkte
🔧 Programmierung

🔧 How Inheritance Works in C# – with Code Examples


📈 17.76 Punkte
🔧 Programmierung

🔧 JavaScript prototypal and classical inheritance


📈 17.76 Punkte
🔧 Programmierung

🕵️ Devil is Virtual: Reversing Virtual Inheritance in C++ Binaries


📈 17.76 Punkte
🕵️ Reverse Engineering

🔧 Inheritance changes for CSS selection styling


📈 17.76 Punkte
🔧 Programmierung

🔧 When to use enums vs. inheritance for modeling object types


📈 17.76 Punkte
🔧 Programmierung

📰 Fake inheritance Nigerian scam now spreading via Telegram


📈 17.76 Punkte
📰 IT Security Nachrichten

matomo