slice 类型的 Go-地道命名

标签 go

当我需要 slice 上的方法时,我必须声明一个新类型。但是我应该给它起什么名字呢?

type SliceSomething []Somethingtype SomethingSlice []Something?

因为它被读作“某物的 slice ”,第一个似乎更好,但自动完成可能更喜欢第二个。

最佳答案

CodeReview wiki page

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

关于 slice 类型的 Go-地道命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27987392/

相关文章:

go - 在Go Channel中尝试范围和关闭

go - 在 Golang 中,如何使用 channel 处理多个 goroutine

linux - Goroutine长时间处于IO等待状态

multithreading - 为什么基于 channel 的 Lock block ?

io - 读取超出缓冲区

macos - 命令失败,fork/exec 权限在 MacOS 上被拒绝?

date - 在golang中使用没有时间的日期的惯用方式是什么?

戈朗 : goroutine infinite-loop

go - 如何在Go中计算一个非常大的文件的SHA256?

运行时错误 : “assignment to entry in nil map”