Lesson 2

Understanding Methods in Go: Extending Data Types with Functionality

Ready, Set, Go: Introduction to Methods

Hello, fellow coder! Brace yourself, for today we will explore Go programming with a focus on methods. Methods are akin to magic spells that allow us to perform various actions on custom data types. We'll delve into method definitions, method declarations, understand the function of receiver functions, and see them in practice. Exciting, right? Let's jump right in!

What's in a Method?

In Go, a method is a function attached to a specific type. Similar to how spells enable wizards to perform magical activities, methods allow us to conduct different operations on specific data types. Consider a racecar — it can accelerate, turn, brake, etc. A Go racecar type could have methods to define these actions.

Understanding the difference between methods and functions is vital. A function is a standalone entity, while a method always belongs to a type. Thus, a method is a special kind of function attached to a specific type.

Declaring Methods: The Articles of Incorporation

Declaring a method in Go follows this protocol:

Go
1func (receiver type) MethodName(parameters) (returnTypes) { 2 // Code 3}

Let's examine a Circle type:

Go
1type Circle struct { 2 radius float64 3} 4 5// declaring a method on the Circle type 6func (c Circle) calculateArea() float64 { 7 return 3.14 * c.radius * c.radius 8}

In this example, we've declared a method calculateArea for the Circle type, which calculates the area when called in a Circle instance.

Unpacking the Receiver Function

Earlier, we emphasized the importance of receivers when declaring methods. A receiver is a specific type tied to every method. Go provides two types of receivers - a value receiver that takes a copy and a pointer receiver that uses a pointer.

Go
1// method with value receiver 2func (c Circle) isLarge() bool { 3 return c.radius > 10 4} 5 6// method with pointer receiver 7func (c *Circle) doubleRadius() { 8 c.radius *= 2 9}

The isLarge method takes a value receiver (which does not modify the original), whereas the doubleRadius method uses a pointer receiver (which does modify the original).

With large structs, using value receivers in methods can potentially be inefficient because a lot of information will be copied each time the method is called.

Deploying Methods: Practical Examples

Let's perform some programming magic and implement the methods on an instance of the Circle:

Go
1func main() { 2 c := Circle{10} 3 4 // Calculating area 5 area := c.calculateArea() 6 fmt.Println("Area:", area) // Prints the area of the circle 7 8 // Checking if circle is large 9 fmt.Println("Is large:", c.isLarge()) // Prints whether the circle is "large" 10 11 // Doubling radius 12 c.doubleRadius() 13 fmt.Println("New radius:", c.radius) // Prints the new radius after doubling 14}

This code outputs:

1Area: 314 2Is large: False 3New Radius: 20
Extending Basic Data Types

Methods in Go can be declared for any data type, not just structs. This flexibility allows Go programmers to extend the functionality of built-in or custom data types beyond their basic operations. Here's how you can attach methods to various data types using Go's method declaration syntax.

Let's start by extending a basic data type. You cannot declare a method with a receiver whose type is defined in another package (including built-in types such as int or float64). So, we will create a local MyFloat type, which is a type alias for float64. We can add methods to this type just like we would with structs.

Go
1type MyFloat float64 2 3// Method to check if MyFloat is positive 4func (f MyFloat) IsPositive() bool { 5 return f > 0 6}

Here, we have defined a new method for our MyFloat type – IsPositive(). It checks if the value is positive or not.

Here is how to use it:

Go
1func main() { 2 var x MyFloat = 10.5 3 fmt.Println(x.IsPositive()) // true 4}

Note that you can't do it with pointers. If you define MyFloat with type MyFloat *float64, it won't work. This restriction is put in place because allowing methods on pointers would complicate the language and its execution model.

Summary and Next Steps

Give yourself a pat on the back for mastering Go methods! You're now ready to apply and practice your newfound knowledge in the upcoming sets of exercises. Remember, just as a wizard's magic spells become more potent with practice, so too will your coding skills with constant hands-on application. Let's forge ahead!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.