Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Inheritance in GO using struct (Embedding)

Posted on August 9, 2023October 9, 2023 by admin

This post desicribes inheritance using struct only. Do visit our Inheritance in Go Complete Guide post for full reference

OOP: Inheritance in GOLANG complete guide

Go supports inheritance by embedding struct or using interface. There are different ways of doing it and each having some limitations. The different ways are:

  1. By using embedded struct – The parent struct is embedded in child struct. The limitation is that subtyping is not possible with this approach. You cannot pass the child struct to a function that expects base. The current post describes this approach.
  2. By using interfaces – Subtyping is possible but the limitation is that one has no way to refer to common properties. Refer this link for more details – Inheritance using Interface
  3. By using interface + struct – This fixes the limitations of above two approach but one limitation is that overriding methods is not possible. But there is a workaround. Refer to this link for more details – Inheritance using interface + struct

Details:

In inheritance using a struct, a base struct is embedded in child struct and base properties and methods can directly be called on child struct. See below code:

package main
import "fmt"
type base struct {
value string
}
func (b *base) say() {
fmt.Println(b.value)
}
type child struct {
base //embedding
style string
}
func check(b base) {
b.say()
}
func main() {
base := base{value: "somevalue"}
child := &child{
base: base,
style: "somestyle",
}
child.say()
//check(child)
}

Output:

somevalue

Limitation:

Subtyping is not supported. You cannot pass the child struct to a function that expects base.

For example in the above code if you uncomment //check(child) it will give compilation error: “cannot use child (type *child) as type base in argument to check”. To fix this we can do inheritance using Interface

  • go
  • golang
  • inheritance
  • interface
  • struct
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP