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.


Solution 1:

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

Solution 2:

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.

Solution 3:

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"