Constant declarations

July 10, 2023

I’m live streaming again! You’ll probably read this after the fact, but if not, come join me live, or catch the replay! I’ll be building a real-world linter from scratch, picking up from the tutorial I did 2 weeks ago.


If you’ve been reading me for a while, you’ll recall we’ve already talked about constants in Go. However, we’ve done it in a slightly more abstract sense; constant literals, and they’re types.

Here we’re finally to the discussion of declaring named constants:

Constant declarations

A constant declaration binds a list of identifiers (the names of the constants) to the values of a list of constant expressions. The number of identifiers must be equal to the number of expressions, and the _n_th identifier on the left is bound to the value of the nth expression on the right.

ConstDecl      = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
ConstSpec      = IdentifierList [ [ Type ] "=" ExpressionList ] .

IdentifierList = identifier { "," identifier } .
ExpressionList = Expression { "," Expression } .

There’s more to be said, which we’ll cover in the coming day or two, but for now, let’s just look at some examples, so this isn’t entirely abstract:

const foo = 123  // A single, untyped numeric constant

const bar int = 234 // A numeric constant of type int

const a, b c = "a", "b", "c" // Three untyped string constants

// I'm sure you can figure these out...
const (
  x = 789
  y = "yup"
  z bool = true
)

Quotes from The Go Programming Language Specification Version of December 15, 2022


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Constant errors

Last week I talked about how to create your own errors. Expanding on that idea, here’s a trick I like to use: Constant errors! Constants in Go are limited to simple underlying types: bool, string, and the numeric types. If we create our own type, which both satisfies the error interface, and has such an underlying type, we can then create constant error values: type errString string func (e errString) Error() string { return string(e) } const ErrTooManyFrogs = errString("too many frogs") Okay.


Constant lengths and expressions

A few of the built-in functions are very special, in that they can evaluate to constant expressions. len and cap are two such functions. But they aren’t always evaluated to constant expressions, sometimes they’re more normal-ish runtime functions. Length and capacity … The expression len(s) is constant if s is a string constant. The expressions len(s) and cap(s) are constants if the type of s is an array or pointer to an array and the expression s does not contain channel receives or (non-constant) function calls; in this case s is not evaluated.


Constant expressions, part III

Did you miss yesterday’s Ask-me-anything session? You probably did. I had about 10-15 people there. But even with a small group, we had a ton of great questions! The Q&A session lasted about an hour, and covered topics such as book recommendations for going deeper into Go, what project to build to learn concurrency, and much more. Catch the replay on YouTube. Let’s continue our discussion of constant expressions, with some more miscellaneous rules:

Get daily content like this in your inbox!

Subscribe