Methods

In the previous example, we saw that a function had an interface as a parameter, but it received structs as an argument. What happened?

When a struct is passed as an argument where an interface is expected, the language implicitly converts your struct to the interface, that is, within the function, we are dealing with an interface in itself.

But how could we then call a method that was defined for structs?

It turns out that when a struct is converted to an interface, you will only have access to the methods that the interface defined.

For this reason, if we try to execute a method in an interface that it hasn't defined, we will generate a panic.

This happens even if the struct that was converted had this method. See the example on the side to understand it in practice.

metodos.go