切片类型的惯用命名 [英] Go-idiomatic naming of slice types

查看:46
本文介绍了切片类型的惯用命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I need methods on slices I have to declare a new type. But what should I name it?

类型为SliceSomething []某些东西还是 type SomethingSlice []某项?

因为它被读为切片",所以第一个看起来更好,但是自动完成可能更喜欢第二个.

Since it's read as "slice of something" the first one seems better, but autocomplete would probably prefer the second one.

推荐答案

CodeReview Wiki页面

Go中的变量名称应该短而不是长.
对于范围有限的局部变量尤其如此.
首选 c 而不是 lineCount .相对于 sliceIndex ,更喜欢 i .

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 "的原因,除了:

That is why you won't find "Slice" often in the go sources, except in:

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 .

So if you have to put 'Slice' in the name, it would be type SomethingSlice []Something rather than type SliceSomething []Something.

这篇关于切片类型的惯用命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆