dongmangzong8006 2015-11-22 17:22
浏览 89
已采纳

在Go中命名Getter的惯用方式

The Effective go has following advice on naming of getters:

Go doesn't provide automatic support for getters and setters. There's nothing wrong with providing getters and setters yourself, and it's often appropriate to do so, but it's neither idiomatic nor necessary to put Get into the getter's name. If you have a field called owner (lower case, unexported), the getter method should be called Owner (upper case, exported), not GetOwner. The use of upper-case names for export provides the hook to discriminate the field from the method. A setter function, if needed, will likely be called SetOwner. Both names read well in practice:

Source: https://golang.org/doc/effective_go.html#Getters

Now, this advice doesn't seem to consistent as the stdlib itself violates this multiple times. enter image description here

As you can see in above screenshot, there are multiple methods which use GetX naming convention which is advised against in the effective go guide.

So the question is is the advice given in guide wrong or these methods are named wrongly & would be fixed in future versions?

  • 写回答

3条回答 默认 最新

  • doukefu1361 2015-11-22 20:32
    关注

    [go-nuts] FunctionName caseinconsistencies

    A lot of the all lowercase names were chosen before we had really figured out what the naming conventions should be. The rule we adopted, which might be worth revisiting later, was that entry points in package os or syscall, which are named after equivalents in C, just had a single capital at the beginning, to avoid needing to decide where the internal capitalizations are in abbreviations like geteuid or getwd or chdir. Names like Readdirnames, which are actual words, might be worth revisiting at some point.

    Russ


    os: inconsistent casing in names #1187

    Is there any sort of rule about the casing of functions used in the "os" package? Looking through, it doesn't sound like it's very easy to recall whether a given function should be called LikeThat or Likethat.

    For instance:

    Mkdir
    MkdirAll
    TempDir
    Getenv
    ForkExec
    Readlink
    ReadAt
    Readdir
    

    It feels very ad-hoc, and hard to recall.

    It's a known issue. It's unplanned.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能