Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go, Why shouldn't use "this" for method receiver name [duplicate]

I use VS Code Go extension.

Here's my code

func (this *MyClass) Xxx() error {}

And It's mention me this

exported method MyClass.Xxx should have comment or be unexported

receiver name should be a reflection of its identity; don't use generic names such as "me", "this", or "self";

like image 879
b.ben Avatar asked Jun 25 '16 06:06

b.ben


1 Answers

As mentioned here

v.Method() is actually syntactic sugar and Go also understands the de-sugared version of it: (T).Method(v). You can see an example here.

package main

type T struct{}

func (t T) Method() {}

func main() {
    t := T{}
    t.Method()    // this is valid
    (T).Method(t) // this too
}

Naming the receiver like any other parameter reflects that it is, in fact, just another parameter quite well.

As Ixrec puts it in this answer:

In other languages the this/self/whatever variable typically has some special properties such as being magically provided by the language, or having special access to private methods (remember Go doesn't have private fields/methods).
Though the "receiver" is still being "magically provided" to some extent, it's so similar to a regular function argument it arguably doesn't count.

like image 61
VonC Avatar answered Nov 04 '22 06:11

VonC