Go naming conventions for const

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

Go naming conventions for const

问题

我正在尝试确定Golang中的const名称是否有命名约定。

个人而言,我倾向于遵循C语言的风格,将它们写成大写,但我在这个页面http://golang.org/doc/effective_go.html上没有找到列出语言命名约定的内容。

英文:

I'm trying to determine whether there is a naming convention for the names of const in Golang.

I personally would tend to follow the C style and write them in upper case, but I haven't found anything on this page http://golang.org/doc/effective_go.html which seems to list some naming conventions for the language.

答案1

得分: 210

标准库使用驼峰命名法,所以我建议你也这样做。首字母的大小写取决于你是否想要导出常量。

以下是一些示例:

  • md5.BlockSize
  • os.O_RDONLY 是一个例外,因为它直接从 POSIX 借用而来。
  • os.PathSeparator
英文:

The standard library uses camel-case, so I advise you do that as well. The first letter is uppercase or lowercase depending on whether you want to export the constant.

A few examples:

  • md5.BlockSize
  • os.O_RDONLY is an exception because it was borrowed directly from POSIX.
  • os.PathSeparator

答案2

得分: 98

> Go Code Review Comments
>
> 这个页面收集了在对Go代码进行审查时常见的评论,以便可以通过简写引用单个详细的解释。
> 这是一个常见错误的清单,而不是一个风格指南。
>
> 您可以将其视为
> http://golang.org/doc/effective_go.html 的补充。
>
> Mixed Caps
>
> 请参阅 http://golang.org/doc/effective_go.html#mixed-caps。即使这违反了其他语言的约定,也适用于Go。例如,一个未导出的常量应该是 maxLength 而不是 MaxLength 或 MAX_LENGTH。


> Effective Go
>
> MixedCaps
>
> 最后,Go中的约定是使用 MixedCaps 或 mixedCaps 而不是下划线来编写多个单词的名称。
>


> The Go Programming Language Specification
>
> Exported identifiers
>
> 为了允许从另一个包中访问标识符,可以将其导出。如果满足以下两个条件,则标识符被导出:
>
> * 标识符名称的第一个字符是一个Unicode大写字母(Unicode类“Lu”);且
>
> * 标识符在包块中声明,或者它是字段名或方法名。
>
> 所有其他标识符都不会被导出。


使用混合大小写。

英文:

> Go Code Review Comments
>
> This page collects common comments made during reviews of Go code, so
> that a single detailed explanation can be referred to by shorthands.
> This is a laundry list of common mistakes, not a style guide.
>
> You can view this as a supplement to
> http://golang.org/doc/effective_go.html.
>
> Mixed Caps
>
> See http://golang.org/doc/effective_go.html#mixed-caps. This applies
> even when it breaks conventions in other languages. For example an
> unexported constant is maxLength not MaxLength or MAX_LENGTH.
>


> Effective Go
>
> MixedCaps
>
> Finally, the convention in Go is to use MixedCaps or mixedCaps rather
> than underscores to write multiword names.
>


> The Go Programming Language Specification
>
> Exported identifiers
>
> An identifier may be exported to permit access to it from another
> package. An identifier is exported if both:
>
> * the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
>
> * the identifier is declared in the package block or it is a field name or method name.
>
> All other identifiers are not exported.


Use mixed caps.

答案3

得分: 31

具体示例。请注意,在常量中声明类型(如果适用)可以帮助编译器。

// 仅对本地文件可见
const localFileConstant string = "具有有限范围的常量值"

// 可导出的常量
const GlobalConstant string = "每个人都可以使用这个"

英文:

Specific examples. Note that declaring the type in the constant (when relevant) can be helpful to the compiler.

// Only visible to the local file
const localFileConstant string = "Constant Value with limited scope"

// Exportable constant
const GlobalConstant string = "Everyone can use this"

huangapple
  • 本文由 发表于 2014年3月27日 21:19:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/22688906.html
匿名

发表评论

匿名网友

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

确定