Video:

Interfaces in Go

June 4, 2021

Course Instructor: Elliot Forbes

Hey Gophers! My name is Elliot and I'm the creator of TutorialEdge and I've been working with Go systems for roughly 5 years now.

In this video, we are going to be looking at interfaces in Go and what they are used for.

Overview

package main

import "fmt"

type Employee interface {
	GetName() string
}

type Engineer struct {
	Name string
}

func (e *Engineer) GetName() string {
	return "Engineer Name: " + e.Name
}

type Manager struct {
	Name string
}

func (m *Manager) GetName() string {
	return "Manager Name: " + m.Name
}

func PrintDetails(e Employee) {
	fmt.Println(e.GetName())
}

func main() {
	engineer := &Engineer{Name: "Elliot"}
	manager := &Manager{Name: "Donna"}
	PrintDetails(engineer)
	PrintDetails(manager)
}

Output

$ go run main.go
Engineer Name: Elliot
Manager Name: Donna