base

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2025 License: MIT Imports: 1 Imported by: 0

README

base

A base class for go that supports inheritance with virtual functions.

See the comments in the base.go file for usage.

Documentation

Overview

Package base describes the Base struct, which provides virtual functions and inheritance similar to that of C++ and Java.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Base

type Base struct {
	// contains filtered or unexported fields
}

Base is a base structure to embed in objects so that it can call its functions as virtual functions. It provides support for a pattern of inheritance that is similar to C++ or Java.

Virtual functions are functions that provide a default implementation but that can be overridden by a subclass. This is generally thought of as an anti-pattern in Go, but for certain applications, it can be really useful.

Every structure that needs to call a virtual function will require a matching interface that defines the virtual functions to be called.

Example:

  // Bird is the base class of all birds.
	 type Bird struct {
	   Base
	 }

	 // BirdI defines the virtual functions of Bird.
	 type BirdI interface {
	   BaseI
	   Call() string
	 }

	 func NewBird() *Bird {
	   b := new(Bird)
	   b.Init(b)
	   return b
	 }

	 func (a *Bird) PrintCall () {
		  fmt.Print(a.this().Call())
	 }

	 // Call here is the default implementation that can be overridden by a subclass.
	 func (a *Bird) Call () string {
	   return "chirp"
	 }

  // this returns a BirdI interface whose functions will be the overriden functions of subclasses.
	 func (a *Bird) this() BirdI {
	   return a.Self().(BirdI)
	 }

	 type Duck struct {
	   Bird
	 }

	 type DuckI interface {
	   BirdI
	 }

	 func NewDuck() *Duck {
	   d := new(Duck)
	   d.Init(d) // must initialize the base class with the new object
	   return d
	 }

	 func (a *Duck) Call () string {
	   return "quack"
	 }

The following code will then print "quack":

NewDuck().PrintCall()

while the following code will the print "chirp".

NewBird().PrintCall()

Be careful when using GobDecode() to restore an encoded object. To restore virtual function ability, you should call Init() on the object after it is decoded.

func (*Base) Init

func (b *Base) Init(self any)

Init captures the object being passed to it so that virtual functions can be called on it.

func (*Base) Self

func (b *Base) Self() any

Self returns the self as an interface object.

func (*Base) String

func (b *Base) String() string

String implements the Stringer interface.

This default functionality outputs the object's type. String is overridable.

func (*Base) TypeOf

func (b *Base) TypeOf() reflect.Type

TypeOf returns the reflection Type of self.

type BaseI

type BaseI interface {
	// TypeOf returns the reflection type of the object. This allows superclasses to determine
	// the type of the subclassing object. The type will not be a pointer to the type, but the type itself.
	TypeOf() reflect.Type
	// String returns a string representation of the object.
	String() string
}

BaseI is an interface representing all objects that embed the Base struct.

It adds the ability for all such objects to get the object's type and a string representation of the object, which by default is the object's type. To be sure to get the object's type even if String() is overridden, use TypeOf().String().

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL