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 (

type Runner interface {

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) {

func main() {
	admin := Admin{

	user := User{
		"Zelalem Mekonen",



  ↑ ↓ to navigate     ↵ to select     Esc to close