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.

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?