Go:切片变量/参数的命名约定是什么?

huangapple go评论79阅读模式
英文:

Go: naming convention for slice variables/params?

问题

在Go语言中,命名切片的惯例是什么?具体来说,你使用复数形式吗?
我注意到Go App Engine不是这样的(例如,它使用key而不是keys):

func GetMulti(c appengine.Context, key []*Key, dst interface{}) error

在我阅读的标准包或文档中,我没有看到类似的东西。是单数还是复数是正常的呢?

英文:

Is the convention for naming slices in Go? Specifically, do you use plurals?
I've noticed that Go App Engine doesn't (eg. it uses key not keys):

func GetMulti(c appengine.Context, key []*Key, dst interface{}) error

I haven't seen anything equivalent in the standard packages or docs I've read. Is singular or plural normal?

答案1

得分: 3

这应该是一个打字错误,我猜测。

切片和数组的名称是复数形式的。在标准库中很容易找到一些示例:CookieJar中的SetCookies函数,os包中的Readdirnames函数,或者os包的变量中的Args变量。

但对于任何变量,使用一个更好地解释其目的的名称。

对于这种全复数的方法,一个明显的例外是argv,它已经有几十年的名称了。

英文:

That should be a typo, I guess.

Names of slices and arrays are plural. It's not hard to find some samples in the standard library: function SetCookies in CookieJar, Readdirnames, or Args variable in the variables of os package.

But for any variable use a name that better explains its purpose.

A clear exception to this all-plural approach is argv which had its name for decades.

答案2

得分: 0

@Mostafa是正确的。在Go中,切片和数组的名称应该是复数形式。在@Ainar-G的示例中,fnsfn的复数形式。请参阅https://stackoverflow.com/a/35648660/12817546。

package main

import "fmt"

func main() {
    var fns []func()
    fns = append(fns, beeper)
    fns = append(fns, pinger)
    for _, fn := range fns {
        fn() //beep-beep ping-ping
    }

}
func beeper() { fmt.Println("beep-beep") }
func pinger() { fmt.Println("ping-ping") }

@Mostafa还指出,名称应该解释其目的。此外,自明的名称使代码更易读。自明的名称对其代码进行评论和文档化。

自明的名称是一致的。也就是说,它们很容易猜测。beeper与"beep-beep"相匹配。自明的名称是准确的。也就是说,它们很容易理解。fns与表示funcfn相匹配。

自明的名称是短的。也就是说,它们很容易输入。短名称要求在声明和使用之间有短的距离。例如,fn等参数可以是一个或两个字符长。对于其他所有情况,可以使用一个短的单词名词。但是,如果距离很大,则可能需要更多的解释。可以使用多个单词的名称mixedCaps。但是不要使用under_score_name

使用的名称可能不够自明。因此,代码可能有太多的//行注释。注释会使代码变得不太易读。重命名标识符,使它们变得更加自明。然后,可以减少//的数量,而不会丢失解释。

对于名称的使用还有其他限制。不能使用Go关键字。不能使用规范的ReadWriteCloseFlushString等单词。名称的第一个字符必须是Unicode字母或_。请记住,像MixedCaps这样的大写名称可以被导出,而type标识符通常是大写的。

要了解更多信息,请参阅https://talks.golang.org/2014/names.slide,https://blog.golang.org/package-names,https://talks.golang.org/2013/bestpractices.slide,https://golang.org/ref/spec#Identifiers和https://golang.org/doc/effective_go.html#names。

英文:

@Mostafa is right. Names of slices and arrays in Go should be a plural. fns is the plural of fn in this example from @Ainar-G. See https://stackoverflow.com/a/35648660/12817546..

package main

import "fmt"

func main() {
	var fns []func()
	fns = append(fns, beeper)
	fns = append(fns, pinger)
	for _, fn := range fns {
		fn() //beep-beep ping-ping
	}

}
func beeper() { fmt.Println("beep-beep") }
func pinger() { fmt.Println("ping-ping") }

@Mostafa is also right that names should explain their purpose. To add to this, self-evident names make code more readable. Self-evident names comment on and document their code.

Self-evident names are consistent. That is they are easy to guess.beeper matches "beep-beep". Self-evident names are accurate. That is they are easy to understand.fns matches fn which denotes a func.

Self-evident names are short. That is they are easy to type. Short names require a short distance between their declaration and their use. Arguments such as fn can be one or two characters-long. For everything else a short one word noun can be used. But sometimes if the distance is great more explanation is needed. A multi-word name mixedCaps can be used. But don't use an under_score_name.

The names used may not be as self-evident as they could be. As a result code may have too many // line comments. Comments can make code less readable. Rename your identifiers so they become more self-evident. And then the number of // can be reduced without loss of explanation.

There are other limits to the use of names. Go keywords cannot be used. Canonical Read, Write, Close, Flush, String etc words should not be used. The first character of a name must be a Unicode letter or a _. Remember capitalized names such as MixedCaps can be exported and type identifiers are typically uppercase.

To find out more have a look at https://talks.golang.org/2014/names.slide, https://blog.golang.org/package-names, https://talks.golang.org/2013/bestpractices.slide, https://golang.org/ref/spec#Identifiers and https://golang.org/doc/effective_go.html#names.

huangapple
  • 本文由 发表于 2012年2月11日 11:35:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/9237610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定