Constants

January 31, 2023

Constants

There are boolean constants, rune constants, integer constants, floating-point constants, complex constants, and string constants. Rune, integer, floating-point, and complex constants are collectively called numeric constants.

So there we have it. A comprehensive list of the available constant types in Go. And all but boolean and string constants are numeric.

Notice that none of the composite types can be constants in Go. You can’t have a constant array, slice, struct, or interface.

A constant value is represented by a rune, integer, floating-point, imaginary, or string literal, an identifier denoting a constant, a constant expression, a conversion with a result that is a constant, or the result value of some built-in functions such as unsafe.Sizeof applied to certain values, cap or len applied to some expressions, real and imag applied to a complex constant and complex applied to numeric constants. The boolean truth values are represented by the predeclared constants true and false. The predeclared identifier iota denotes an integer constant.

In general, complex constants are a form of constant expression and are discussed in that section.

Okay, wow. There’s a lot in that section. But it’s not that complex. In summary, a constant value is represented by:

  • An appropriate literal

    const x = 123 // A numeric literal
    
  • An identifier denoting another constant

    const x = 123
    
    const y = x // x is an identifier denoting a constant
    
  • A constant expression (which we’ll discuss into later)

    const x = 1 + 2 + 3 // a constant expression
    
  • A conversion, with a constant result

    const x = int(123) // x is an integer constant
    
    const y = int64(x) // int64(x) converts x to an int64, and has a constant result
    
  • Or the result of some built-in functions such as unsafe.Sizeof, len, or cap.

    x := [3]int{}    // x is an array of int, with length 3
    
    const y = len(x) // Although x is not a constant, the result of len(x) is,
                     // because it is known at compile time.
    

We’ll talk more about the boolean constant values, and the special constant iota in the future.

Quotes from The Go Programming Language Specification, Version of June 29, 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