How To Use Interfaces in Go

Written by Gopher Guides

Writing flexible, reusable, and modular code is vital for developing versatile programs. Working in this way ensures code is easier to maintain by avoiding the need to make the same change in multiple places. How you accomplish this varies from language to language. For instance, inheritance) is a common approach that is used in languages such as Java, C++, C#, and more.

Developers can also attain those same design goals through composition. Composition is a way to combine objects or data types into more complex ones. This is the approach that Go uses to promote code reuse, modularity, and flexibility. Interfaces in Go provide a method of organizing complex compositions, and learning how to use them will allow you to create common, reusable code.

In this article, we will learn how to compose custom types that have common behaviors, which will allow us to reuse our code. We’ll also learn how to implement interfaces for our own custom types that will satisfy interfaces defined from another package.

Defining a Behavior

One of the core implementations of composition is the use of interfaces. An interface defines a behavior of a type. One of the most commonly used interfaces in the Go standard library is the fmt.Stringer interface:

  1. type Stringer interface {
  2. String() string
  3. }

The first line of code defines a type called Stringer. It then states that it is an interface. Just like defining a struct, Go uses curly braces ({}) to surround the definition of the interface. In comparison to defining structs, we only define the interface’s behavior; that is, “what can this type do”.

In the case of the Stringer interface, the only behavior is the String() method. The method takes no arguments and returns a string.

Next, let’s look at some code that has the fmt.Stringer behavior:

main.go

  1. package main
  2. import "fmt"
  3. type Article struct {
  4. Title string
  5. Author string
  6. }
  7. func (a Article) String() string {
  8. return fmt.Sprintf("The %q article was written by %s.", a.Title, a.Author)
  9. }
  10. func main() {
  11. a := Article{
  12. Title: "Understanding Interfaces in Go",
  13. Author: "Sammy Shark",
  14. }
  15. fmt.Println(a.String())
  16. }

The first thing we do is create a new type called Article. This type has a Title and an Author field and both are of the string data type:

main.go

  1. ...
  2. type Article struct {
  3. Title string
  4. Author string
  5. }
  6. ...

Next, we define a method called String on the Article type. The String method will return a string that represents the Article type:

main.go

  1. ...
  2. func (a Article) String() string {
  3. return fmt.Sprintf("The %q article was written by %s.", a.Title, a.Author)
  4. }
  5. ...

Then, in our main function, we create an instance of the Article type and assign it to the variable called a. We provide the values of "Understanding Interfaces in Go" for the Title field, and "Sammy Shark" for the Author field:

main.go

  1. ...
  2. a := Article{
  3. Title: "Understanding Interfaces in Go",
  4. Author: "Sammy Shark",
  5. }
  6. ...

Then, we print out the result of the String method by calling fmt.Println and passing in the result of the a.String() method call:

main.go

  1. ...
  2. fmt.Println(a.String())

After running the program you’ll see the following output:

Output

  1. The "Understanding Interfaces in Go" article was written by Sammy Shark.

So far, we haven’t used an interface, but we did create a type that had a behavior. That behavior matched the fmt.Stringer interface. Next, let’s see how we can use that behavior to make our code more reusable.

Defining an Interface

Now that we have our type defined with the desired behavior, we can look at how to use that behavior.

Before we do that, however, let’s look at what we would need to do if we wanted to call the String method from the Article type in a function:

main.go

  1. package main
  2. import "fmt"
  3. type Article struct {
  4. Title string
  5. Author string
  6. }
  7. func (a Article) String() string {
  8. return fmt.Sprintf("The %q article was written by %s.", a.Title, a.Author)
  9. }
  10. func main() {
  11. a := Article{
  12. Title: "Understanding Interfaces in Go",
  13. Author: "Sammy Shark",
  14. }
  15. Print(a)
  16. }
  17. func Print(a Article) {
  18. fmt.Println(a.String())
  19. }

In this code we add a new function called Print that takes an Article as an argument. Notice that the only thing the Print function does is call the String method. Because of this, we could instead define an interface to pass to the function:

main.go

  1. package main
  2. import "fmt"
  3. type Article struct {
  4. Title string
  5. Author string
  6. }
  7. func (a Article) String() string {
  8. return fmt.Sprintf("The %q article was written by %s.", a.Title, a.Author)
  9. }
  10. type Stringer interface {
  11. String() string
  12. }
  13. func main() {
  14. a := Article{
  15. Title: "Understanding Interfaces in Go",
  16. Author: "Sammy Shark",
  17. }
  18. Print(a)
  19. }
  20. func Print(s Stringer) {
  21. fmt.Println(s.String())
  22. }

Here we created an interface called Stringer:

main.go

  1. ...
  2. type Stringer interface {
  3. String() string
  4. }
  5. ...

The Stringer interface has only one method, called String() that returns a string. A method is a special function that is scoped to a specific type in Go. Unlike a function, a method can only be called from the instance of the type it was defined on.

We then update the signature of the Print method to take a Stringer, and not a concrete type of Article. Because the compiler knows that a Stringer interface defines the String method, it will only accept types that also have the String method.

Now we can use the Print method with anything that satisfies the Stringer interface. Let’s create another type to demonstrate this:

main.go

  1. package main
  2. import "fmt"
  3. type Article struct {
  4. Title string
  5. Author string
  6. }
  7. func (a Article) String() string {
  8. return fmt.Sprintf("The %q article was written by %s.", a.Title, a.Author)
  9. }
  10. type Book struct {
  11. Title string
  12. Author string
  13. Pages int
  14. }
  15. func (b Book) String() string {
  16. return fmt.Sprintf("The %q book was written by %s.", b.Title, b.Author)
  17. }
  18. type Stringer interface {
  19. String() string
  20. }
  21. func main() {
  22. a := Article{
  23. Title: "Understanding Interfaces in Go",
  24. Author: "Sammy Shark",
  25. }
  26. Print(a)
  27. b := Book{
  28. Title: "All About Go",
  29. Author: "Jenny Dolphin",
  30. Pages: 25,
  31. }
  32. Print(b)
  33. }
  34. func Print(s Stringer) {
  35. fmt.Println(s.String())
  36. }

We now add a second type called Book. It also has the String method defined. This means it also satisfies the Stringer interface. Because of this, we can also send it to our Print function:

Output

  1. The "Understanding Interfaces in Go" article was written by Sammy Shark.
  2. The "All About Go" book was written by Jenny Dolphin. It has 25 pages.

So far, we have demonstrated how to use just a single interface. However, an interface can have more than one behavior defined. Next, we’ll see how we can make our interfaces more versatile by declaring more methods.

Multiple Behaviors in an Interface

One of the core tenants of writing Go code is to write small, concise types and compose them up to larger, more complex types. The same is true when composing interfaces. To see how we build up an interface, we’ll first start by defining only one interface. We’ll define two shapes, a Circle and Square, and they will both define a method called Area. This method will return the geometric area of their respective shapes:

main.go

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type Circle struct {
  7. Radius float64
  8. }
  9. func (c Circle) Area() float64 {
  10. return math.Pi * math.Pow(c.Radius, 2)
  11. }
  12. type Square struct {
  13. Width float64
  14. Height float64
  15. }
  16. func (s Square) Area() float64 {
  17. return s.Width * s.Height
  18. }
  19. type Sizer interface {
  20. Area() float64
  21. }
  22. func main() {
  23. c := Circle{Radius: 10}
  24. s := Square{Height: 10, Width: 5}
  25. l := Less(c, s)
  26. fmt.Printf("%+v is the smallest\n", l)
  27. }
  28. func Less(s1, s2 Sizer) Sizer {
  29. if s1.Area() < s2.Area() {
  30. return s1
  31. }
  32. return s2
  33. }

Because each type declares the Area method, we can create an interface that defines that behavior. We create the following Sizer interface:

main.go

  1. ...
  2. type Sizer interface {
  3. Area() float64
  4. }
  5. ...

We then define a function called Less that takes two Sizer and returns the smallest one:

main.go

  1. ...
  2. func Less(s1, s2 Sizer) Sizer {
  3. if s1.Area() < s2.Area() {
  4. return s1
  5. }
  6. return s2
  7. }
  8. ...

Notice that we not only accept both arguments as the type Sizer, but we also return the result as a Sizer as well. This means that we no longer return a Square or a Circle, but the interface of Sizer.

Finally, we print out what had the smallest area:

Output

  1. {Width:5 Height:10} is the smallest

Next, let’s add another behavior to each type. This time we’ll add the String() method that returns a string. This will satisfy the fmt.Stringer interface:

main.go

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type Circle struct {
  7. Radius float64
  8. }
  9. func (c Circle) Area() float64 {
  10. return math.Pi * math.Pow(c.Radius, 2)
  11. }
  12. func (c Circle) String() string {
  13. return fmt.Sprintf("Circle {Radius: %.2f}", c.Radius)
  14. }
  15. type Square struct {
  16. Width float64
  17. Height float64
  18. }
  19. func (s Square) Area() float64 {
  20. return s.Width * s.Height
  21. }
  22. func (s Square) String() string {
  23. return fmt.Sprintf("Square {Width: %.2f, Height: %.2f}", s.Width, s.Height)
  24. }
  25. type Sizer interface {
  26. Area() float64
  27. }
  28. type Shaper interface {
  29. Sizer
  30. fmt.Stringer
  31. }
  32. func main() {
  33. c := Circle{Radius: 10}
  34. PrintArea(c)
  35. s := Square{Height: 10, Width: 5}
  36. PrintArea(s)
  37. l := Less(c, s)
  38. fmt.Printf("%v is the smallest\n", l)
  39. }
  40. func Less(s1, s2 Sizer) Sizer {
  41. if s1.Area() < s2.Area() {
  42. return s1
  43. }
  44. return s2
  45. }
  46. func PrintArea(s Shaper) {
  47. fmt.Printf("area of %s is %.2f\n", s.String(), s.Area())
  48. }

Because both the Circle and the Square type implement both the Area and String methods, we can now create another interface to describe that wider set of behavior. To do this, we’ll create an interface called Shaper. We’ll compose this of the Sizer interface and the fmt.Stringer interface:

main.go

  1. ...
  2. type Shaper interface {
  3. Sizer
  4. fmt.Stringer
  5. }
  6. ...

Note: It is considered idiomatic to try to name your interface by ending in er, such as fmt.Stringer, io.Writer, etc. This is why we named our interface Shaper, and not Shape.

Now we can create a function called PrintArea that takes a Shaper as an argument. This means that we can call both methods on the passed in value for both the Area and String method:

main.go

  1. ...
  2. func PrintArea(s Shaper) {
  3. fmt.Printf("area of %s is %.2f\n", s.String(), s.Area())
  4. }

If we run the program, we will receive the following output:

Output

  1. area of Circle {Radius: 10.00} is 314.16
  2. area of Square {Width: 5.00, Height: 10.00} is 50.00
  3. Square {Width: 5.00, Height: 10.00} is the smallest

We have now seen how we can create smaller interfaces and build them up into larger ones as needed. While we could have started with the larger interface and passed it to all of our functions, it is considered best practice to send only the smallest interface to a function that is needed. This typically results in clearer code, as anything that accepts a specific smaller interface only intends to work with that defined behavior.

For example, if we passed Shaper to the Less function, we may assume that it is going to call both the Area and String methods. However, since we only intend to call the Area method, it makes the Less function clear as we know that we can only call the Area method of any argument passed to it.

Conclusion

We have seen how creating smaller interfaces and building them up to larger ones allows us to share only what we need to a function or method. We also learned that we can compose our interfaces from other interfaces, including those defined from other packages, not just our packages.

If you’d like to learn more about the Go programming language, check out the entire How To Code in Go series.