9

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";

Community
  • 1
  • 1
kitta
  • 1,723
  • 3
  • 23
  • 33
  • Hi this question was answered: http://programmers.stackexchange.com/questions/286406/use-of-this-in-golang http://stackoverflow.com/questions/29028512/go-this-keywordh – sauroter Jun 25 '16 at 06:44
  • 1
    In short words: The `this` means `inheritance`. `Inheritance` means `abstract` and `virtual` methods. That is, with the `Inheritance` you can write `this.callAbstractMethod()`. Go language does not support `inheritance`. This means that `receiver` is not a `this` because `this` always means something that maybe not a `this` but an `other` (eg. `super`). In Go language there are no `super` instances under the `this` instance since in Go language not exists inheritance as such. – mezoni Jun 25 '16 at 08:34

1 Answers1

13

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250