In Go is naming the receiver variable 'self' misleading or good practice?

前端 未结 6 1441
青春惊慌失措
青春惊慌失措 2020-12-01 02:42

I have seen a fair amount of blogs & videos on Go and as far as I recall, none of the authors use \'self\' or \'this\' for the receiver variable when writing methods. H

6条回答
  •  春和景丽
    2020-12-01 03:12

    Look at the source code for Go commands and Go standard packages for exemplars of good programming style. The name self is not used for Go method receivers.

    Go Source Code

    The name self is a Python convention. Go is not Python.

    The Python Tutorial

    9.4. Random Remarks

    Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python. Note, however, that by not following the convention your code may be less readable to other Python programmers, and it is also conceivable that a class browser program might be written that relies upon such a convention.

提交回复
热议问题