Welcome back.
Let me tell you an essential knowledge unlike some other Object-Oriented-Programming languages Go does not have classes the
methods can be attached to almost any type.
It doesn't have to be a struct type.
For example you can attach methods to the basic types such as an integer string or float or you can
attach them to bare types like arrays or structs or you can attach them to pointer bearing types like slices
maps or channels.
You can even attach methods to functions.
This last one may sound kind of weird but in some situations, it can become very useful for the types
on the right side.
Don't use pointer receivers with them as you know they already carry a pointer with themselves OK.
Let's talk about the example.
I'm going to show you.
Initially there will be a simple slice without any method in the end it will have a printing method
that can print all the games in the slice.
OK let's get started.
First of all I'm going to remove everything but the variables from here since I've got a few games to
sell let's create a slice to hold all of them together each element of the slice is a pointer to a game
value.
I'm going to add Minecraft and Tetris to the slice
since this is a slice of game pointers.
I cannot add a book value to it because of that.
I'm going to comment out the Moby Dick which is a book value.
Now I'm going to print all the elements using a loop and I'm going to call the print method of the next
game value.
That's it.
Let's try it nice.
It lists the games.
Let's organize the code a little bit.
I'm going to create a new file list.
Now I'm going to create a method to list all the games.
Let's copy the code from the main function
this method receives a game slice using the L receiver variable.
So I just need to change the items variable like this. Oops!
There is an error.
It says that the receiver type is invalid.
It's because the game slice is not a defined type.
It's an unnamed type.
So it doesn't belong to any package a type and its methods should be in the same package.
That's why I need to create a new type.
So first I need to define a new type in the current package.
To do that I'm going to declare a new type using the game slice type.
Now we've got a new type with a new name.
I need to attach the print method to the list type.
So I'm going to change the receiver type to the list type.
Let's get back to the main function.
How can I use the list type here?
First I'm going to create a new variable and I'm going to convert the item slice to the list type I
can do so because the underlying type of the list type is a game slice.
Both the list type and the items slice have the same underlying type thanks to the list type the variable
my has got a print method on the fly.
Let's use it.
OK let's save and try it.
It works.
Excellent here I add the methods of the list type to the items slice on the fly I just needed to convert
the slice to the list type imagine how powerful this can be.
This means that you don't have to add methods to a type right from the start.
Using this trick you can always add methods later on.
OK let's take a look at another example let's say there are no games in the store so I'm going to set
the variable my to nil it runs without any problems but it doesn't print anything.
Let's make it prettier.
The underlying type of the list type is a slice.
So I can use it as a slice.
For example I can check whether the receiver is empty or or not. If so,
I'm going to print a message and return.
Let's try it.
Now it says that the store is closed.
Nice here.
The variable my is nil but I could run the print method on it so you can call methods on a nil value
as well.
For now let's comment out the nil assignment.
As I said you can attach methods to almost any type.
As an example I will create a new float type which I call money.
It will format the desired amount of dollars when I run the method.
It will return a String value like this.
I will use the money type for the game and book types.
Okay let's get back to the coding editor. Let's create the money type in a new file.
I'm going to declare a new type using the float type.
Remember a type and its methods should be in the same package.
That is why I needed to create a new type.
Now I have a money type.
Let's add a method to it let's name it string it returns a string let's use the sprintf function to
format and return the dollar amount I will use the money type for the price fields of game and book
types.
OK let's go to the game type and use this new type there.
First I'm going to change the price field to money to return the price I need to run the string method
on the price field.
I can do so because now the price field is a money type and the money type has a string method since
it returns a string.
I'm going to change the float verb to a string.
Think of course there is an error in the discount method.
The price field is a money but the ratio argument is float.
So the types don't match I can solve this problem by converting the expression on the right to the money
type okay.
Nice.
Let's do the same for the book.
I'll change the price field first and I'm going to call the string method.
That's it.
Let's try it.
Nice.
Now the price fields are printing themselves as you can see.
You can also attach methods to basic types congrats.
This was the methods.
See you in the next lecture.