ChatGPT解决这个技术问题 Extra ChatGPT

Go naming conventions for const

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.

I'd love to write constants in the FOO_BAR_BAZ style for readability, but unfortunately that affects the visibility of the constant and it isn't common. I've just had to swallow my pride and accept the convention even if I don't like it.

J
John Topley

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


It's also important to determine if you want your const element exposed to other packages. If you use UpperCamelCase or ALL_CAPS you'll be exporting it outside of your package. For this reason, I stick to lowerCamelCase for private const variables, and I recall reading this recommendation from someone relatively close to the Go project (or perhaps even in official documentation--I forget where).
It is Pascal Case
I agree this is the correct way in Go, but it makes it harder to read in cases where two words are capitalized and you are not allowed to use underscore, for example HTTPID instead of HTTP_ID. I had LED_A, LED_B etc. in my code, and now have to change to LEDA, LEDB etc. to follow the convention and so that lint doesn't complain. I think the latter is less readable, especially when you join two longer acronyms.
First I have heard of "upper" camel-case but it's probably better than pascal-case which is the historical name for VariablesLikeThis
@NileshKesar, Yes, I think that's clearer and how I would do it. Although I didn't specifically mention it, I was referring Google's style for which there is a linter as well: github.com/golang/go/wiki/CodeReviewComments#initialisms. According to that it should be LEDA LEDB. But of course you can adopt your own style, like you suggest. I just wanted to stick to an established style.
D
DallaRosa

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.


S
Speedy99

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"