Check out a free preview of the full Go for JavaScript Developers course:
The "Methods Solution" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna live codes the solution to the exercise.

Get Unlimited Access Now

Transcript from the "Methods Solution" Lesson

>> Okay, so let's work through the solution to exercise 9A. So if you're starting with the code that exists in methods.go, you'll see that we have our User struct and our Group struct. And then our describeUser function here. So to quick pop that into a method, I'm going to insert our User struct as our receiver.

[00:00:24] Cool. And then I'm gonna copy the same code from lines 20 and 21 in our describeUser function. Pop it into our describe method, which needs the method name describe. And then for describeGroup. We're going to change describeGroup to be a method that takes a Group struct. And here I'm gonna use a pointer.

[00:00:55] And the difference here, and this is kind of one of the examples I was mentioning earlier. Here we are wanting to modify the actual state of that struct. We wanna update whether or not the space available is true or false. So as a general rule, if you're gonna be modifying that in a permanent way.

[00:01:08] Using a pointer value allows you to get at that memory and actually change the values there. So I'll use a pointer for group, and it's returning a string. I still want it to be called describe. And then I'm gonna copy the code that lives in our describeGroup function.

[00:01:31] Place that into our describe method, make sure it returns a string. And then I'll go ahead and comment out. I'm actually just gonna delete them, delete the old functions for some, clean up a little bit. [COUGH] I was gonna delete describeUser. Cool, so now we have two methods that both take their respective structs.

[00:02:01] One is a user, one is a group. And then they still just print out that string that describes either of those things. So we'll pop down here to our main function, we need to modify these two functions which no longer work. So we'll say, we can PrintLine on group.describe.

[00:02:23] And user, we'll say user1.describe, I'll spell describe correctly. Cool, save. I don't have a user1 apparently, just a U, so never mind. We'll say u.subscribe. Then if we go run section nine in our code directory with methods, you'll see that we're kinda back to square one with just different syntax for making those methods happen.

[00:03:04] Those will also become a little bit more apparent as we talk about interfaces. But you'll notice that both of these can have the same method name describe. If I were to have made two functions that were called described, we would have gotten an error because Go does name space where you can only have one function name of the same kind in a file.

[00:03:21] So describe that takes a user, and describe that takes a group would have blown up. So methods do allow you to have the same function name. And since this function or method is kind of accomplishing the same goal of printing out a description of whatever you pass in.

[00:03:33] It's helpful and makes a lot more sense to have the same function name. It does the same thing, just depends on what's being passed in.