gpt4 book ai didi

slice 类型的 Go-地道命名

转载 作者:IT王子 更新时间:2023-10-29 02:05:35 26 4
gpt4 key购买 nike

当我需要 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com