198

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.

1
  • 16
    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.
    – byxor
    Jul 5, 2018 at 20:09

3 Answers 3

215

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
6
  • 24
    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).
    – weberc2
    Aug 7, 2014 at 14:01
  • 4
    It is Pascal Case
    – Mirza
    Oct 13, 2020 at 3:37
  • 3
    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.
    – Bjorn
    Jan 25, 2021 at 7:51
  • 1
    First I have heard of "upper" camel-case but it's probably better than pascal-case which is the historical name for VariablesLikeThis May 6, 2021 at 19:09
  • 2
    @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.
    – Bjorn
    Mar 8, 2022 at 17:51
98

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.

0
32

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.