Naming Constants

I am trying to determine if a naming convention exists for const names in the Golang.

I personally would like to follow the C style and write in uppercase, but I did not find anything on this page http://golang.org/doc/effective_go.html , which seems to list some naming conventions for the language.

+49
go naming-conventions const
Mar 27 '14 at 13:19
source share
3 answers

The standard library uses a camel case, so I advise you to do this. The first letter is in upper or lower case, depending on whether you want to export the constant.

A few examples:

  • md5.BlockSize
  • os.O_RDONLY is an exception since it was borrowed directly from POSIX.
  • os.PathSeparator
+47
Mar 27 '14 at 13:20
source share

Comments on the Go Code Review

This page summarizes the general comments made during the reviews of Go code, so that one detailed explanation can be attributed to the abbreviation. This is a list of common mistakes, not a style guide.

You can view this as an add-on 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 violates agreements in other languages. For example, the Optional constant is maxLength, not MaxLength or MAX_LENGTH.




Effective way

Mixedcaps

Finally, the convention in Go is to use MixedCaps or mixedCaps than underscores to write verbose names.




Go programming language specification

Exported Identifiers

An identifier can be exported to allow access to it from another package. The identifier is exported if both:

  • the first character of the identifier name is the Unicode uppercase letter (Unicode class "Lu"); and

  • an identifier is declared in a package block or is it a field name or a method name.

All other identifiers are not exported.




Use mixed caps.

+42
Mar 27 '14 at 14:01
source share

Specific examples. Note that declaring a type in a constant (when appropriate) may be useful 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" 
+1
May 13 '16 at 17:39
source share



All Articles