Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go naming conventions for const

People also ask

What is the naming convention for constants?

Constants should be written in uppercase characters separated by underscores. Constant names may also contain digits if appropriate, but not as the first character.

Is Golang camelCase or snake case?

In Golang, any variable (or a function) with an identifier starting with an upper-case letter (example, CamelCase) is made public (accessible) to all other packages in your program, whereas those starting with a lower-case letter (example, camelCase) is not accessible to any package except the one it is being declared ...

What is constant Go?

In Go, const is a keyword introducing a name for a scalar value such as 2 or 3.14159 or "scrumptious" . Such values, named or otherwise, are called constants in Go. Constants can also be created by expressions built from constants, such as 2+3 or 2+3i or math. Pi/2 or ("go"+"pher") .

Why does Go use short variable names?

It's to discourage writing go as if you're writing a language where those variables have special meaning.


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"