"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Can Embedding Improve Complex Structural Hierarchy Implementation in Go?

How Can Embedding Improve Complex Structural Hierarchy Implementation in Go?

Posted on 2025-03-24
Browse:821

How Can Embedding Improve Complex Structural Hierarchy Implementation in Go?

Idiomatic Implementation of Complex Structural Hierarchies in Go

Go's lack of inheritance and support for embedding make the representation of complex structural hierarchies non-trivial. The Go compiler's use of empty methods in its AST implementation has raised questions about its efficacy.

Understanding Empty Methods

While not essential, empty methods serve two key purposes:

  1. Type Assertion: They force Go's type system to check that a type implements a specific interface, ensuring that incompatible types cannot be assigned to one another.
  2. Documentation: They explicitly document the implementation of an interface by a type, making the relationship clear.

Leveraging Embedding

Embedding allows a struct to incorporate another struct's fields and methods, creating a form of inheritance. By embedding the appropriate structs in a hierarchical manner, we can reduce the need for empty methods.

Object-Immovable-Movable Hierarchy

Consider the following hierarchy:

Object
--Immovable
----Building
----Mountain
--Movable
----Car
----Bike

Object Implementation:

type Object interface {
  object()
}

type ObjectImpl struct {}

func (o *ObjectImpl) object() {}

Immovable Implementation:

type Immovable interface {
  Object
  immovable()
}

type ImmovableImpl struct {
  ObjectImpl // Embedded Object implementation
}

func (i *ImmovableImpl) immovable() {}

Building Implementation:

type Building struct {
  ImmovableImpl // Embedded Immovable implementation
  // Additional Building-specific fields
}

Movable Implementation:

type Movable interface {
  Object
  movable()
}

type MovableImpl struct {
  ObjectImpl // Embedded Object implementation
}

func (m *MovableImpl) movable() {}

Car Implementation:

type Car struct {
  MovableImpl // Embedded Movable implementation
  // Additional Car-specific fields
}

Example Usage:

// Building cannot be assigned to a Movable-typed variable because it does not implement the Movable interface.
var movable Movable = Building{}

// However, it can be assigned to an Object-typed variable because both Immovable and Movable implement Object.
var object Object = Building{}

Advantages of Embedding:

  1. Reduced number of empty methods, leading to cleaner and more straightforward code.
  2. Clear delineation of structural relationships through embedded structs.
  3. Inherited methods and fields across different types, simplifying implementation.
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3