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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.