educative.io

Possible mistake on "Introduction to Methods" in The Way to Go course

However, there is a way around this: you can define an alias for that type (int, float, …), and then define a method for that alias.

I’ve tried doing an alias as in this snippet: https://goplay.tools/snippet/CEXmtLUZXuf
And it doesn’t work.

Hello @Anisia_Iorgulescu ,
The reason you are getting an error is because the alias you defined for time.Time is not defined correctly. If you look at the code again given in the lesson for time.Time its alias is defined in a struct.
Hope this helps

The entire paragraph before the code is:

You get the same error for a type defined in another, thus also non-local package. However, there is a way around this: you can define an alias for that type (int, float, …), and then define a method for that alias. Or, embed the type as an unknown type in a new struct, like in the following example. Of course, this method is only valid for the alias type.

So the example in the code presents the second option (“Or, embed the type as an unknown type in a new struct”). However, from the paragraph before it looks like there is also the option of an alias, which I couldn’t replicate.

Hello @Anisia_Iorgulescu ,
You are right the code represents the second option. As far as the first option and the structure of the paragraph is concerned, I will discuss them with the author.

1 Like