Introduction

Go interfaces are beneficial to Go developers because they:

  • Allow interfaces to separate mechanism from behavior.
  • Increase flexibility of function parameters.
  • Enable mocking of function parameters.

With the addition of generics in Go 1.18, interfaces can be used to constrain the types of values a generic function will accept. To dive deeper into Go interfaces, we invite you to join Miki in his latest series dedicated to Go interfaces.

In this video, Miki has two functions that perform the same operation, but return different types. To remove the duplicate code, Miki will replace the two functions with a generic function that will allow him to specify which type the function should return. While implementing this, Miki demonstrates how the compiler may not recognise a type and methods to circumvent this issue. Watch and learn how to define a generic function and limit the types accepted by the function with an interface.

Things you will learn in this video

  • Define a generic function.
  • Explicitly pass a type to a generic function.
  • Constrain accepted types of generic functions with interfaces.

Video

Trusted by top technology companies

We've built our reputation as educators and bring that mentality to every project. When you partner with us, your team will learn best practices and grow along the way.

30,000+

Engineers Trained

1,000+

Companies Worldwide

12+

Years in Business