Lesson 13. Methods

After reading lesson 13, you’ll be able to

  • Declare new types
  • Rewrite functions as methods

Methods are like functions that enhance types with additional behavior. Before you can declare a method, you need to declare a new type. This lesson takes the kelvinToCelsius function from lesson 12 and transforms it into a type with methods.

At first it may look like methods are just a different syntax for doing what functions already do, and you would be right. Methods provide another way to organize code, an arguably nicer way for the examples in this lesson. Later lessons, those in unit 5 in particular, demonstrate how methods can be combined with other language features to bring new capabilities.

Consider this

Get Get Programming with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.