Welcome to Part Six.
You will learn about methods and interfaces.
First you will learn the methods.
After that you will learn the interfaces.
Lastly you will learn the well known interfaces let's start with the methods methods enhance types
with additional behavior I will create a couple of types and a few functions.
After that I will show you how to convert the code to an object oriented style using methods.
Suppose you want to sell books to keep everything organized.
I'm going to create a new file.
First I'm going to declare a new book type OK.
Now I can create a function that prints a book.
Lastly I'm going to print the title and price of a given book value it's ready let's use it from the
main function.
Let's create a book I'm gonna say Moby Dick and price ten dollars.
OK let's printed OK let's run the program.
Nice.
It works.
Let's say you decided to sell computer games as well to do that.
I will create a game type instead of typing the same code over and over again.
Let's duplicate the book type instead and I will change the book type to the game as you can see.
You cannot create a function with the same name in the same package.
I have declared the print book function before. It belongs to the namespace of package main.
So I need to change the name of the function.
I also need to change the input parameter to the game type.
Lastly I'm going to change the variables B to G okay.
As you can see it's very similar to the book type here.
I created a game type and a function for printing the game type is ready let's use it from the main
function.
I'm going to create a new game
Let's print it Okay let's run the code.
It works.
So far so good.
As you can see there is a printing function for each type here.
This is a functional style of programming.
Now we can talk about the problems of this style of programming and reasons why you might want to use
methods instead of functions.
The book type has only two fields title and price.
You can create a book value like this as you can see the Moby Dick variable contains the data but has
no behavior.
I have written only one function for the book type but assume that there will be two functions.
The first one prints a book the last one discounts the price of a book.
Both take book values and their primary purpose is to work with books.
So why don't we combine the data and behavior together I added the functions to the book type the print
book and discount functions have become the methods of the book type.
As you can see I no longer need to pass a book value to these methods because Go will automatically
pass the value of a book to them.
You will see an example shortly.
No worries. Now the book type has both data and behavior.
The book with its data and behavior better represents a real world book product by the way the methods
of a type are called the method set of that type.
Here the method set of the book type consists of printing and discounting methods.
Let me explain how you can attach methods to a type.
Let's take a look at the print book function.
We wrote earlier it takes a book value as an argument but the print book function is separate from the
book to its composite the separate.
You can run it like this. When you run it,
the variable Moby Dick is being sent to the printBook function as a copy.
Now let's attach the function to the book type.
Now the print book function has become a method.
Let's talk a little bit about the method declarations syntax.
The book argument has become something called a receiver.
It's just a fancy name for an input parameter.
It's just syntactic sugar.
If you are coming from other object oriented languages this term may be a familiar to you.
Here we say receiver because a method automatically receives the value of a type.
As you know input parameters are variables A receiver is also an input parameter so a receiver is also
a variable.
Let's take a look at its parts.
This is the type of the receiver variable.
This is the name of the receiver variable.
Now the book type has a print book method since the Moby Dick is also a book type.
You can call the print book method on it.
When you do so, Go
passes the Moby Dick variable to the print method as a hidden argument before the method executes Go
copies the value of Moby Dick to the receiver variable B so you can use the variable B to print the
book.
Let me give you one more tip about the receiver names.
We frequently use receiver names so we name them using abbreviations.
But you can name them however you want here the printBook method belongs to the book type.
Therefore I used only the first letter of the book type.
Let me show you how you can attach a method to the book type in the coding editor.
Let's start by cutting the first parameter from here.
And paste it to the left in parentheses.
That's it.
Ready as you can see the receiver is actually an input parameter written just before the function name.
We use the method syntax to make Go understand that the printBook function is a method of the book type.
Now we can use it in the main function but now Go is looking for the print book function in the main
package but it cannot find it because the print book function now belongs to the book type.
So instead I'm going to use a value of the book type Moby Dick and I will type a dot.
Remember the dot operator selects a name from a namespace.
Here it selects from the namespace of the book type when I pressed the control and space keys together
my editor lists only the fields and methods of the book type.
The print book Method is here.
I can call it like a function.
The only difference is that I need to run it through a value here.
I execute it through the Moby Dick variable behind the scenes.
Go sends the Moby Dick to the print book method as a receiver.
Let's try it as you can see it works as before.
Let's create a method for the game type as well.
Let's start by cutting the first parameter and paste it within parentheses to the left.
That's it.
Let's run the methods from the main function.
Now the print game method belongs to the game type.
That's why you can see it here.
Let's try it.
It also works as before.
Nice.
Now let's try it with another game value.
I'm going to add a new game value like this.
And I'm going to print it as well.
Let's try it as you can see when I call the print game method with the Minecraft variable.
It uses the Minecraft variable but when I run the same method with the Tetris variable this time it
uses the Tetris variable instead.
It's because the print game method receives a different game value depending on who is calling it.
It's like passing the game variable to the print game function.
The only difference is that gold passes.
The game value as the receiver.
Don't forget the receiver is a variable.
I will show you the first benefit of using methods.
As I said types have their own namespaces for example since the print book method belongs to the book
type I can simply say print I mean I don't need to say print book anymore it's because there can be
methods with the same names among different types.
Let's go to the game type and do the same thing there.
I'm going to change the name just to print.
Now we can go back to the main function and use the new method names there.
Here I'm going to call the print method instead and another one here let's try it nice.
It works on the left the book type has a print method you can run the first print method on the left
only through book values on the right side.
The game type has a print method as well.
However this time you can run the second print method only with game values.
So this means that a method belongs to a single type I can use the same method names because they belong
to different types.
Each type has its own namespace.
If those methods were functions I wouldn't be able to use the same names.
All right that's all for now.
See you in the next lecture.