Transcript from the "Methods" Lesson
>> So let's talk about methods. So when we were talking about structs, we're defining each type of a manually defined or manually defining a type. We've got our user type that has an ID, first name, last name, email. We have a group type that has a few attributes on that.
[00:00:15] And then we have a couple functions that we wrote to describe each of those types, the first one takes in a user type that we can print out a particular sentence describing the user. And the second one was describing the group type. And it had kind of a similar behavior but different actual string printed out.
[00:00:33] So so far we've been using these kind of basic functions that are expecting some type of argument that is a manually defined type struct. And then our main function, we kind of just called all of those described functions and printed out those strings to the console. So that's kind of where we're starting with and we're going to flip over and talk about methods and how they differ from functions.
[00:00:52] These two boxes are going to show the differences between both the implementation of a method versus a function, and how those are called on a particular struct. So the top left hand corner, that second function describe user is going to look like the function that we talked about yesterday, where it takes in that user struct argument and then it's bringing back out a string.
[00:01:11] The difference between a method and a function is that instead of accepting an argument as a struct, you're calling a method on an instance of that struct. So in the top version, we're talking about a method. In the parentheses that happened right after the keyword funk, that indicates what the method is going to be called on.
[00:01:30] So what is the receiving struct of that method. That top left corner there, you're seeing that the describe method is going to be called on an instance of a user. And when the describe method sees that user type, it's going to fire off the code that's in the curly braces.
[00:01:44] And so we're going to print that same string to the console. So similar behavior, you take in a struct, you print out some stuff, the difference being that a method is going to be particularly concerned about state, and what kind of state or information that that struct is getting passed in with.
[00:02:20] And then in the bottom right hand corner is the difference between how you would call either a function call on a user or a method on a user. So that first describe, which is set equal to our function describe user and we're passing our user instance. And the second is how you would call a method where you were actually calling the describe method on an instance of a user.
[00:02:41] So instead of passing it as an argument, you're calling a method on an instance. We're gonna talk a little bit about why this is important and kinda what that looks like in the next couple of examples here. Cool, so let's hop over to methods.go and make this change in a go base.
[00:02:57] Cool, so so far I'm working with describe function describeUser function, excuse me. And we don't have any methods in this file whatsoever. So we're gonna implement that code we just saw on the slide, and transform our describeUser function into a method. So we know that for methods, we need a receiver, what are we calling this method on?
[00:03:21] And I'm gonna call it on a user. You've noticed that I've chosen to use a pointer type here. Typically with methods, you want to like modify the state that's on the instance of the thing you just passed in. So for instance, hey, call describe on a user and then you add additional information or modify some of the attributes on whatever type you just passed in.
[00:03:49] We aren't actually doing any modification in this particular method. We're just printing out a string. So that the pointer here isn't entirely crucial, but believe it for demonstration purposes. So I'm going to call it describe. And now it no longer takes any arguments because we are calling this method on an instance.
[00:04:07] We're no longer passing anything directly to it. We're still getting back that string. And then I'm gonna copy lines 14 to 15 and just pop that into our method. And then the main function. Instead of line 25, where we are passing our describe user function a user instance, I'm going to set a new variable desk to user.describe.
[00:04:37] The advantages of this will be much more apparent when we kind of dive into interfaces. But the behavior of the describe function, whether it's in the describe user or describe group is essentially the same type of behavior. You're still like spitting out a string that describes something you pass into it.
[00:04:56] So in terms of like code encapsulation, and kind of demonstrating that the thing you pass in doesn't super matter as long as it's doing a particular job of describing that thing. Readability and kind of using idiomatic code to make what you're trying to accomplish more apparent. And methods have a lot more strength in that regard.
[00:05:13] And we'll talk about what that looks like a little bit more with interfaces, too. Kind of the rule of thumb would be if you are concerned about state and want to modify state, you're using a method. Functions tend to have to be much more repeatable. Duplicate a lot of code and you're not concerned with modifying state quite as much.