From the course: Learning Go

Define functions as methods of custom types - Go Tutorial

From the course: Learning Go

Define functions as methods of custom types

- In Go, you can attach functions to your custom types. They're then referred to as methods. In a more completely Object Oriented language such as Java, you'd say that each method is a member of a class. In Go, a method is a member of a type. In this example, I'm going back to my dog struct, that I used in a previous exercise. And I'll show you how to attach a method to the struct. First I need to add a new field to my dog struct, I'll call it sound, and I'll set it with a type of string. And then I'll come back up here to where I'm initializing my poodle object and I'll pass in a sound of Woof! Now I'm ready to create a custom method. Down at the bottom, I'll create a new function. I'll start with the keyword func as always. And before I put in the name of the function, I'm going to pass in what's known as, the receiver. Inside parentheses, I'll add an identifier for the receiver and then the type. And I'm saying I'm passing in a reference to a dog object. Then I'll give the function a name of Speak. And within the function, I can now reference that dog object with the identifier. I'll call fmt.print line, and I'll pass in d.Sound. Now, in my main function, I can call that function, with poodle.Speak. And it's a function, so I need the parentheses at the end. And when I run the code, I get Woof! At the end of the output. Notice this squiggly warning condition. This is telling me that I should have a comment, before all exported functions. Remember, that an exported function is one that's public to the rest of the application, and it's indicated by the initial uppercase character in the name. The function has a required format. Start with the double slashes, and then the name of the function, and then the word is, and everything after that is up to you. I'll set that to Speak as how the dog speaks, and when I save my changes, the warning goes away, and the code works exactly the same as it did before. You can also change the exported or public fields of an object, and then call that method again. So I'll come back up here to my main function, and I'll say poodle.Sound, and I'll set it to a value of Arf! Then I'll create a copy of the command to make the dog speak, and run the code. And this time I get Woof! And then Arf! Now Go doesn't support method overrides. Each method has to have its own unique name, but like all functions, methods can return values, just declare the type assigned to the method. So I'm going to create another method down here. I'm going to call this one speak three times. And this time within the function, I'm going to reassign the value of the sound field, like this. First, I need to pass in the receiver, just like I did with the other function. And then within the function, I can say d.Sound. And then I'll use a function from the format package called Sprintf. And this is just like the printf function, but instead of outputting to the console, it returns a string. The string will have three placeholders of percent V, percent V, percent V. And then I need three values to match those placeholders, so I'll pass in d.Sound three times. And then I put that value with fmt.Print line, and d.Sound. I'll copy and paste my comment, and then I'll modify it for this function. And then I'll go back to my main function, and I'll call poodle.SpeakThreeTimes. And when I run the code, the dog barks three times. Now here's an interesting thing. Let's say that I were to call that function twice. Within the function, I'm reassigning the value of the sound field. So what do we think is going to happen? Will the dog bark three times, or nine times? And the answer is that it only barks three times. That's because, when you pass the dog object in as the receiver, a copy is made of it, it's not a reference. If you want it to be a reference, you can use pointers. But within the function, this is a brand new copy of the object, so when I modify the sound field, I'm not modifying the version that was created in the main function. The ability to create custom methods for your own types, makes Go behave more like a fully Object Oriented language, even without this sort of type inheritance that you find in C++, and Java. A struct can have as many different methods as it needs, to accomplish your applications requirements.

Contents