切片类型的 Go-地道命名
Go-idiomatic naming of slice types
当我需要切片上的方法时,我必须声明一个新类型。但是我应该给它起什么名字呢?
type SliceSomething []Something
或 type SomethingSlice []Something
?
因为读作 "slice of something" 第一个似乎更好,但自动完成可能更喜欢第二个。
查看 Go source code 以获得普遍认可的成语。
Variable names in Go should be short rather than long.
This is especially true for local variables with limited scope.
Prefer c
to lineCount
. Prefer i
to sliceIndex
.
The basic rule: the further from its declaration that a name is used, the more descriptive the name must be.
这就是为什么您不会经常在 go 源中找到“Slice
”的原因,除了:
encoding/gob/encoder_test.go:335: type recursiveSlice []recursiveSlice
encoding/json/encode_test.go:107: type renamedByteSlice []byte
encoding/json/encode_test.go:108: type renamedRenamedByteSlice []renamedByte
regexp/onepass.go:283: type runeSlice []rune
sort/sort.go:233: type IntSlice []int
sort/sort.go:243: type Float64Slice []float64
sort/sort.go:258: type StringSlice []string
unicode/maketables.go:1118: type runeSlice []rune
因此,如果您在名称中添加“Slice
”,则为type SomethingSlice []Something
而不是 type SliceSomething []Something
.
当我需要切片上的方法时,我必须声明一个新类型。但是我应该给它起什么名字呢?
type SliceSomething []Something
或 type SomethingSlice []Something
?
因为读作 "slice of something" 第一个似乎更好,但自动完成可能更喜欢第二个。
查看 Go source code 以获得普遍认可的成语。
Variable names in Go should be short rather than long.
This is especially true for local variables with limited scope.
Preferc
tolineCount
. Preferi
tosliceIndex
.The basic rule: the further from its declaration that a name is used, the more descriptive the name must be.
这就是为什么您不会经常在 go 源中找到“Slice
”的原因,除了:
encoding/gob/encoder_test.go:335: type recursiveSlice []recursiveSlice
encoding/json/encode_test.go:107: type renamedByteSlice []byte
encoding/json/encode_test.go:108: type renamedRenamedByteSlice []renamedByte
regexp/onepass.go:283: type runeSlice []rune
sort/sort.go:233: type IntSlice []int
sort/sort.go:243: type Float64Slice []float64
sort/sort.go:258: type StringSlice []string
unicode/maketables.go:1118: type runeSlice []rune
因此,如果您在名称中添加“Slice
”,则为type SomethingSlice []Something
而不是 type SliceSomething []Something
.