Getting Started With Go Part 6: Interfaces

This is part 6 in a series called Getting Started With Go. If you would like to check out parts 1-5 you may find them here:

  1. Part 1: Hello World
  2. Part 2: Variables & Constants
  3. Part 3: Functions
  4. Part 4: Arrays & Slices
  5. Part 5: Pointers & Structs 
  6. Part 6: Interfaces (you are here now)

In this post we’re going to do a quick intro on what interfaces are and how they are used. Interfaces are a core part of Go and offer a unique way to identify behavior for different things we implement throughout the course of an application. There are 3 key things to remember that will help when working with interfaces. 

  1. Interfaces are types
  2. Interfaces define behavior via a group of functions
  3. Any struct with the same functions as an interface can become that type of interface

Let’s demonstrate these things to make them a little more clear. Here, I have declared an ExtremeAthlete interface with a Train() function. 

type ExtremeAthlete interface {
Train()
}

Since it’s a type, we start off by using the keyword type to declare our interface. The behavior defined by this interface will be to train. Next, let’s create a few more structs and see how they can automatically be referred to as an ExtremeAthlete.

type BasketballPlayer struct {
  Name string
}

type Swimmer struct {
  Name string
}

type Runner struct {
  Name string
}

With our three structs modeled after different types of athletes all we need to do is create the function that matches the method signature of the Train() function within ExtremeAthlete.

func (b BasketballPlayer) Train() {
  fmt.Println(b.Name +  " took 5000 practice shots on the shooting gun")
}

func (s Swimmer) Train() {
  fmt.Println(s.Name + " swam 200 laps in the pool")
}

func (r Runner) Train() {
  fmt.Println(r.Name + " ran 15 miles on the treadmill")
}

Lastly, I’m creating a function called Practice that takes an ExtremeAthlete.

func Practice(e ExtremeAthlete) {
  e.Train()
}

In doing so, we will be able to pass in a BasketballPlayer, Runner, or Swimmer. When hooked up in main, it would look something like this:

func main() {
  lebronJames := BasketballPlayer{Name:"Lebron James"}
  michaelPhelps := Swimmer{Name:"Michael Phelps"}
  davidGoggins := Runner{Name:"David Goggins"}

  Practice(lebronJames)
  Practice(michaelPhelps)
  Practice(davidGoggins)
}

You get this result when running the above code:

Lebron James took 5000 practice shots on the shooting gun
Michael Phelps swam 200 laps in the pool
David Goggins ran 15 miles on the treadmill

That’s it! These extra types we created (BasketballPlayer, Swimmer, Runner) are able to be referenced as an ExtremeAthlete because they all have that one train method in common. Nothing else is needed. The Tour of Go website has a section on interfaces that is a bit more interactive if you want to play with a few different examples. It may help to see a few different examples and use cases. However, if you feel like you have a good grasp on interfaces then great. As usual feel free to reach out or comment with any comments, questions, and concerns. 

Leave a Comment

Your email address will not be published. Required fields are marked *