Essential Go Object oriented programming  Suggest an edit

Interface & polymorphism

Interface defines a set of methods on a struct. It defines behavior.

Interface cannot define data.

It allows functions to accept multiple implementation of the same functionality.

This is similar (but not identical) to the notion of virtual methods in C++.

package main

import (
	"fmt"
)

type Runner interface {
	Run()
}

type Admin struct {
	Username, Password string
}

func (admin Admin) Run() {
	fmt.Println("Admin ==> Run()")
}

type User struct {
	ID              uint64
	FullName, Email string
}

func (user User) Run() {
	fmt.Println("User ==> Run()")
}

// RunnerExample takes any type that fullfils the Runner interface
func RunnerExample(r Runner) {
	r.Run()
}

func main() {
	admin := Admin{
		"zola",
		"supersecretpassword",
	}

	user := User{
		1,
		"Zelalem Mekonen",
		"zola.mk.27@gmail.com",
	}

	RunnerExample(admin)

	RunnerExample(user)
}
Admin ==> Run()
User ==> Run()

  ↑ ↓ to navigate     ↵ to select     Esc to close