Introduction

In episode 1, Miki had two functions that performed the similar operation, but returned different types. To refactor this, Miki rewrote both functions as a generic function that allowed him to specify the type to be returned during invocation. In some cases, the compiler may not recognise a type, thus, Miki gives a few pointers on manually telling a generic function which type to use. After demonstrating how to return different types with a generic function, Miki limited the types his generic function would accept with an interface.

In this video, Miki will take a closer look at the performance implications of interfaces since “there’s no such thing as a free lunch.” The first step he’ll take is to define a struct to represent weather and a function to return said type with a hard-coded value. Miki chose this example as it illustrates how a function’s variable can escape to the heap due to the use of interfaces. To better frame the previous statement, Go’s logging functions accept interface types for arguments and this is done to increase the universality of the logging function. Watch and learn how you can benchmark your code, view optimizations done by the compiler and pinpoint the variables being allocated to heap.

Things you will learn in this video

  • Performance implications of interfaces.
  • Go build flags to find code escaping to the heap.
  • Comments you can add to your functions that will prevent the compiler from inlining a function.
  • Performance impact of allocating to heap.

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

14+

Years in Business