douxian1892 2014-07-20 05:08
浏览 262
已采纳

为什么go语言的方法语法很奇怪[关闭]

I couldn't well understand why the go developers settled on a syntax like func (t Type) MethodName() for methods. I couldn't digest this fact especially after reading this and considering the fact that go is minimalistic. Wouldn't a simpler syntax like func Type.MethodName() or func Type::MethodName() been sufficient with the object accessed using an implicit argument like this or self. Or am I missing any advantages offered by the current syntax?

  • 写回答

1条回答 默认 最新

  • dongtai419309 2014-07-20 05:15
    关注

    The goal of that particular syntax is very particular to the language Go and cannot easily be mapped to other language syntax:

    This syntax allows you to define a method set

    A type may have a method set associated with it. The method set of an interface type is its interface.

    • The method set of any other type T consists of all methods declared with receiver type T.
    • The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T).

    Further rules apply to structs containing anonymous fields, as described in the section on struct types. Any other type has an empty method set. In a method set, each method must have a unique non-blank method name.

    The method set of a type determines the interfaces that the type implements and the methods that can be called using a receiver of that type.

    It isn't so much an "advantage" than it is a Go feature allowing to easily extend a type with new methods.

    See for instance "What are some examples of Go interfaces?".


    twotwotwo adds in the comments:

    There are two particular things that an explicit receiver declaration lets you do:

    1. decide that some methods will get pointer receivers and others (e.g., non-mutating methods on small structs) don't, and
    2. choose a context-specific name instead of 'self' or 'this' (e.g., you might have a func (srv *Server)...).
      Context-specific names are considered good style in Go

    See Wiki CodeReviewComments

    The name of a method's receiver should be a reflection of its identity; often a one or two letter abbreviation of its type suffices (such as "c" or "cl" for "Client").

    Don't use generic names such as "me", "this" or "self", identifiers typical of object-oriented languages that place more emphasis on methods as opposed to functions.
    The name need not be as descriptive as a that of a method argument, as its role is obvious and serves no documentary purpose. It can be very short as it will appear on almost every line of every method of the type; familiarity admits brevity.
    Be consistent, too: if you call the receiver "c" in one method, don't call it "cl" in another.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角