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.
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.
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
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.
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"
Success story sharing
const
element exposed to other packages. If you useUpperCamelCase
orALL_CAPS
you'll be exporting it outside of your package. For this reason, I stick tolowerCamelCase
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).HTTPID
instead ofHTTP_ID
. I hadLED_A
,LED_B
etc. in my code, and now have to change toLEDA
,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.