Constants And Variables

This article will introduce constant and variable declarations in Go. The concept of untyped values and explicit conversions will also be introduced.

The literals introduced in the last article are all called unnamed constants (or literal constants), except false and true, which are two predeclared (built-in) named constants. Custom named constant declarations will be be introduced below in this article.

Untyped Values And Typed Values

In Go, some values are untyped. An untyped value means the type of the value has not been confirmed yet. On the contrary, the type of a typed value is determined.

For most untyped values, each of them has one default type. The predeclared nil is the only untyped value which has no default type. We will learn more about nil in other Go 101 articles later.

All literal constants (unnamed constants) are untyped values. In fact, in Go, most untyped values are literal constants and named constants (that is introduced in this article). The other untyped values include the just mentioned nil and some boolean results returned by some operations which will be introduced in other articles later.

The default type of a literal constant is determined by its literal form.

Explicit Conversions Of Untyped Constants

Like many other languages, Go also supports value conversions. We can use the form T(v) to convert a value v to the type denoted by T (or simply speaking, type T). Go compilers view T(v) as a typed value of type T. Surely, for a certain type T, to make the conversion T(v) legal, the value v can't be arbitrary. If the conversion T(v) is legal,

The following mentioned rules apply for both the literal constans introduced in the last article and the untyped named constants which will be introduced soon.

For an untyped constant value v, there are two scenarios where T(v) is legal.
  1. v (or the literal denoted by v) is representable as a value of a basic type T. The result value is a typed constant of type T.
  2. The default type of vis an integer type (int or rune) and T is a string type. The result of T(v) is a string of type T and contains the UTF-8 representation of the integer as a Unicode break point. Integer values outside the range of valid Unicode code points result a string represented by "\uFFFD" (a.k.a., "\xef\xbf\xbd"). 0xFFFD is the code point for the Unicode replacement character. The result string of a conversion from an integer always contains one and only one rune.

In fact, the second scenario doesn't require v to be a constant. If v is a constant, then the result of the conversion is also a constant, otherwise, the result is not a constant.

For example, the following conversions are all legal.
complex128(1 + -1e-1000i)  // 1.0+0.0i of type complex128. Rounding happens here.
float32(0.49999999)        // 0.5 of type float32. Rounding happens here.
float32(17000000000000000) // rounding happens here.
float32(123)
uint(1.0)
int8(-123)
int16(6+0i)
complex128(789)

string(65)          // "A"
string('A')         // "A"
string('\u68ee')    // "森"
string(-1)          // "\uFFFD"
string(0xFFFD)      // "\uFFFD"
string(0x2FFFFFFFF) // "\uFFFD"

And the following conversions are all illegal.
int(1.23)        // 1.23 is not representable as a value of int.
uint8(-1)        // -1 is not representable as a value of uint8.
float64(1+2i)    // 1+2i is not representable as a value of float64.

float64(-1e1000)         // constant -1e+1000 overflows float64.
int(0x10000000000000000) // constant 0x10000000000000000 overflows int.

string(65.0)  // the default type of 65.0 is float64 (not an integer type).
string(66+0i) // the default type of 66+0i is complex128 (not an integer type).

From the above examples, we know that an untype constant, (for example -1e1000 and 0x10000000000000000), may even not be able to represent as a value of its default type.

Please note, sometimes, the form of explicit conversions must be written as (T)(v) to avoid ambiguities. For example, T is an unnamed pointer type. We can learn more about pointers in the article pointers in Go later.

We will learn more explicit conversion rules later in other Go 101 articles.

Introduction Of Type Deductions In Go

Go supports type deduction. In other words, in many circumstances, programmers don't need to explicitly specify the types of some values in code. Go compilers will deduce the types for these values by context.

Type deduction is also often called type inference.

In Go code, if a place needs a value of a certain type and an untyped value (often a constant) is representable as a value of the certain type, then the untyped value can be used in the place. Go compilers will view the untyped value as a typed value of the certain type. Such places include an operand in an operator operation, an argument in a function call, a destination value or a source value in an assignment, etc.

Some circumstances have no requirements on the types of the used values. If an untyped value is used in such a circumstance, Go compilers will treat the untyped value as a typed value of its default type.

The two type deduction cases can be viewed as implicit conversions. Please note that, there is no the implicit conversion concept in Go specification. This concept is only used in Go 101.

The below constant and variable declaration sections will show some type deduction cases. More type deduction rules and cases will be introduced in other articles.

Constant Declarations

Unnamed constants are all boolean, numeric and string values. Like unnamed constants, named constants can also be only boolean, numeric and string values. The keyword const is used to declare named constants. The following program contains some constant declarations.

package main

// Declare two individual constants.
// Yes, non-ASCII letters can be used in identifiers.
const π = 3.1416
const Pi = π // equivalent to: Pi == 3.1416

// Declare multiple constants as a group by using ().
const (
	No         = !Yes
	Yes        = true
	MaxDegrees = 360
	Unit       = "radian"
)

func main() {
	// Declare multiple constants in one code line.
	const DoublePi, HalfPi, Unit2 = π * 2, π * 0.5, "degree"
}

Go specification calls each of the lines containing a = symbol in the above constant declaration group as a constant specification.

In the above example, the * symbol is the multiplication operator and the ! symbol is the boolean-not operator. Operators will be introduced in the next article, common operators.

The = symbol means "assign". So each constant specification in a constant declaration can be viewed as an assignment. However, more strictly speaking, we should interpret each constant specification as a declared identifier is bound to a corresponding basic value literal. Please read the last section in the current article for more explanations.

In the above example, the name constants π and Pi are both bound to the literal 3.1416. The two named constants may be used at many places in code. Without constant declarations, the literal 3.1416 would be populated at those places. If we want to change the literal to 3.14 later, many places need to be modified. With the help of constant declarations, the literal 3.1416 will only appear in one constant declaration, so only one place needs to be modified. This is the main purpose of constant declarations.

Later, we use the terminology non-constant values to denote the values who are not constants. The to be introduced variables below, all belong to one kind of non-constant values.

Please note that, constants can be declared both at package level (out of any function body) and in function bodies. The constants declared in function bodies are called local constants. The variables declared out of any function body are called package-level constants. We also often call package-level constants as global constants.

The declaration orders of two package-level constants are not important. In the above example, the declaration orders of No and Yes can be exchanged.

All constants declared in the last example are untyped. The default type of a named untyped constant is the same as the literal it denotes.

Typed Named Constants

We can declare typed constants, typed constants are all named. In the following example, all the four declared constants are typed values. The types of X and Y are both float32 and the types of A and B are both int64.

const X float32 = 3.14

const (
	A, B int64   = -3, 5
	Y    float32 = 2.718
)

If multiple typed constants are declared in the same constant specification, then their types must be the same, just as the constants A and B in the above example.

We can also use explicit conversions to provide enough information for Go compilers to deduce the types of typed named constants. The above code snippet is equivalent to the following one, in which X, Y, A and B are all typed constants.

const X = float32(3.14)

const (
	A, B = int64(-3), int64(5)
	Y    = float32(2.718)
)

If a basic value literal is bound to a typed constant, the basic value literal must be representable as a value of the type of the constant. The following typed constant declarations are invalid.
const a uint8 = 256             // error: 256 overflows uint8
const b = uint8(255) + uint8(1) // error: 256 overflows uint8
const c = int8(-128) / int8(-1) // error: 128 overflows int8
const MaxUint_a = uint(^0)      // error: -1 overflows uint
const MaxUint_b uint = ^0       // error: -1 overflows uint

In the above and following examples ^ is bitwise not operator.

The following typed constant declaration is valid on 64-bit OSes, but invalid on 32-bit OSes. For each uint value has only 32 bits on 32-bit OSes. (1 << 64) - 1 is not representable as 32-bit values.
const MaxUint uint = (1 << 64) - 1
Then how to declare a typed uint constant and bind the largest uint value to it? Use the following way instead.
const MaxUint = ^uint(0)
Similarly, we can declare a typed int constant and bind the largest int value to it.
const MaxInt = int(^uint(0) >> 1)
A similar method can be used to get the number of bits of a native word, and check the current OS is 32-bit or 64-bit.
const NativeWordBits = 32 << (^uint(0) >> 63) // 64 or 32
const Is64bitOS = ^uint(0) >> 63 != 0
const Is32bitOS = ^uint(0) >> 32 == 0

Autocomplete In Constant Declarations

In a group-style constant declaration, except the first constant specification, other constant specifications can be incomplete. An incomplete constant specification doesn't contain the = symbol. Compilers will autocomplete the incomplete lines for us by copying the missing part from the first preceding complete constant specification. For example, at compile time, compilers will automatically complete the following code

const (
	X float32 = 3.14
	Y                // here must be one identifier
	Z                // here must be one identifier

	A, B = "Go", "language"
	C, _
	// In the above line, the blank identifier is required.
)
as
const (
	X float32 = 3.14
	Y float32 = 3.14
	Z float32 = 3.14

	A, B = "Go", "language"
	C, _ = "Go", "language"
)

iota In Constant Declarations

The autocomplete feature plus the iota constant generator feature brings much convenience to Go programming. iota is a predeclared constant which can only be used in other constant declarations. It is declared as

const iota = 0

But the value of an iota in code may be not always 0. When the predeclared iota constant is used in a custom constant declaration, at compile time, within the custom constant declaration, its value will be reset to 0 at the first constant specification of each group of constants and will increase 1 constant specification by constant specification. In other words, in the nth constant specification of a constant declaration, the value of iota is n (starting from zero). So iota is only useful in group-style constant declarations.

Here is an example using both the autocomplete and the iota constant generator features. Please read the comments to get what will happen at compile time. The + symbol in this example is the addition operator.

package main

func main() {
	const (
		k = 3 // now, iota == 0
	
		m float32 = iota + 0.1 // m float32 = 1 + 0.1
		n                      // n float32 = 2 + 0.1
	
		p = 9             // now, iota == 3
		q = iota * 2      // q = 4 * 2
		_                 // _ = 5 * 2
		r                 // r = 6 * 2
		s, t = iota, iota // s, t = 7, 7
		u, v              // u, v = 8, 8
		_, w              // _, w = 9, 9
	)

	const x = iota // x = 0
	const (
		y = iota // y = 0
		z        // z = 1
	)

	println(m, n)          // +1.100000e+000 +2.100000e+000
	println(q, r)          // 8 12
	println(s, t, u, v, w) // 7 7 8 8 9
	println(x, y, z)       // 0 0 1
}

The above example is just to demo the rules of the iota constant generator feature. Surely, in practice, we should use it in more meaningful ways. For example,

const (
	Failed = iota - 1 // == -1
	Unknown           // == 0
	Succeeded         // == 1
)

const (
	Readable = 1 << iota // == 1
	Writable             // == 2
	Executable           // == 4
)

Here, the - symbol is the subtraction operator, and the << symbol is the left-shift operator. Both of these operators will be introduced in the next article.

Variables, Variable Declarations And Value Assignments

Variables are named values. Variables are stored in memory at run time. The value represented by a variable can be modified at run time.

All variables are typed values. When declaring a variable, there must be sufficient information provided for compilers to deduce the type of the variable.

The variables declared within function bodies are called local variables. The variables declared out of any function body are called package-level variables. We also often call package-level variables as global variables.

There are two basic variable declaration forms, the standard one and the short one. The short form can only be used to declare local variables.

Standard Variable Declaration Forms

Each standard declaration starts with the var keyword, which is followed by the declared variable name. Variable names must be identifiers.

The following are some full standard declaration forms. In these declarations, the types and initial values of the declared variables are all specified.

var lang, website string = "Go", "https://golang.org"
var compiled, dynamic bool = true, false
var announceYear int = 2008

As we have found, multiple variables can be declared together in one variable declaration. Please note, there can be just one type specified in a variable declaration. So the types of the multiple variables declared in the same declaration line must be identical.

Full standard variable declaration forms are seldom used in practice, since they are verbose. In practice, the two standard variable declaration variant forms introduced below are used more often. In the two variants, either the types or the initial values of the declared variables are absent.

The following are some standard variable declarations without specifying variable types. Compilers will deduce the types of the declared variables as the types (or default types) of their respective initial values. The following declarations are equivalent to the above ones in fact. Please note, in the following declarations, the types of the multiple variables declared in the same declaration line can be different.

// The types of the lang and dynamic variables will be
// deduced as "string" and "bool" by compilers, respectively.
var lang, dynamic = "Go", false

// The types of the compiled and announceYear variables will
// be deduced as "bool" and "int" by compilers, respectively.
var compiled, announceYear = true, 2009

// The types of the website variable will be deduced
// as "string" by compilers.
var website = "https://golang.org"

The type deductions in the above example can be viewed as implicit conversions.

The following are some standard declarations without specifying variable initial values. In these declarations, all declared variables are initialized as the zero values of their respective types.

var lang, website string      // both are initialized as blank strings.
var interpreted, dynamic bool // both are initialized as false.
var n int                     // is initialized as 0.

Multiple variables can be grouped into one standard form declaration by using (). For example:

var (
	lang, bornYear, compiled     = "Go", 2007, true
	announceAt, releaseAt    int = 2009, 2012
	createdBy, website       string
)

The above example is formatted by using the go fmt command in the official Go SDK. In the above example, each of the three lines are enclosed in () this is known as variable specification.

Generally, declaring related variables together will make code more readable.

Pure Value Assignments

In the above variable declarations, the sign = means assignment. Once a variable is declared, we can modify its value by using pure value assignments. Like variable declarations, multiple values can be assigned in a pure assignment.

Later, if we say x is assignable to y, what we mean is y is mutable, and the types of x and y are identical, x can be implicitly converted to the type of y, or y is the blank identifier _. If y is not the blank identifier _, it must be either addressable, or a map index syntax. (Memory addresses and map types will be introduced and explained later.)

Constants are immutable and unaddressable values, so they can't appear at the left sides of pure value assignments. Variables are all addressable, so they can appear at the left sides of pure value assignments.

Example:
const N = 123
var x int
var y, z float32

N = 789 // error: constant N is not modifiable
y = N   // okay: N is implicitly converted to (deduced as a) float32
x = y   // error: type mismatch
x = N   // okay: N is implicitly converted to (deduced as an) int
y = x   // error: type mismatch
z = y   // okay
_ = y   // okay

z, y = y, z               // okay
_, y = y, z               // okay
z, _ = y, z               // okay
_, _ = y, z               // okay
x, y = 69, 1.23           // okay
x, y = y, x               // error: type mismatch
x, y = int(y), float32(x) // okay

The code at last line in the above example uses explicit conversions to make the corresponding destination and source values matched. The explicit conversion rules for non-constant numeric values are introduced below.

Go doesn't support assignment chain. For example, the following code is illegal.

var a, b int
a = b = 123 // syntax error

Short Variable Declaration Forms

We can also use short variable declaration forms to declare variables. Short variable declarations can only be used to declare local variables. Let's view an example which uses some short variable declarations.

package main

func main() {
	// Both lang and year are two new declared variables.
	lang, year := "Go language", 2007

	// Only createdBy is a new declared variable.
	// The year variable has already been declared before,
	// so here its value is just modified.
	year, createdBy := 2009, "Google Research"

	// This is a pure assignment.
	lang, year = "Go", 2012

	print(lang, " is created by ", createdBy)
	println(", and released at year", year)
}

There are several differences between short and standard variable declarations.

  1. In the short declaration form, the var keyword and variable types must be omitted.
  2. The assignment sign must be := instead of =.
  3. In the short variable declaration, old variables and new variables can mix at the left of :=. But there must be at least one new variable at the left.

Please note, comparing to pure assignments, there is a limit for short variable declarations. In a short variable declaration, all items at the left of the := sign must pure identifiers. This means some other assignable items, which will be introduced in other articles, can't appear at the left of :=. These items include qualified identifiers, container elements, pointer dereferences and struct field selectors. Pure assignments have no such limit.

About The Terminology "Assignment"

Later, when the word "assignment" is mentioned, it means a pure assignment, a short variable declaration, or a variable specification with initial values in a standard variable declaration.

Each Local Declared Variable Must Be Used At Least Once Effectively

Please note, the standard Go compiler and gccgo both don't allow local variables declared but not used. Package-level variables have no such limit.

If a local variable is only ever used as destination values, it will also be viewed as unused.

For example, in the following program, r is only used as destination.

package main

var x, y, z = 123, true, "foo" // package-level variables.

func main() {
	var q, r = 789, false
	r, s := true, "bar"
	r = y // r is unused.
	x = q // q is used.
}

Compiling the above program will result to the following compilation errors (assume the source file is name example-unused.go):

./example-unused.go:6:6: r declared and not used
./example-unused.go:7:16: s declared and not used

The fix is easy, we can assign r and s to blank identifiers to avoid compilation errors.

package main

var x, y, z = 123, true, "foo"

func main() {
	var q, r = 789, false
	r, s := true, "bar"
	r = y
	x = q

	_, _ = r, s // make r and s used.
}

Generally, the above fix is not recommended to be used in production code. It should be used in development/debug phase only. It is not a good habit to leave unused local variables in code, for unused local variables have negative effects on both code readability and program execution performance.

Dependency Relations Of Package-Level Variables Affect Their Initialization Order

For the following example,

var x, y = a+1, 5         // 8 5
var a, b, c = b+1, c+1, y // 7 6 5 8

the initialization order of the package-level variables are y = 5, c = y, b = c+1, a = b+1, and x = a+1.

Here, the + symbol is the addition operator, which will be introduced in the next article.

Package-level variables can't be depended circularly in their declaration. The following code fails to compile.

var x, y = y, x

Value Addressability

In Go, some values are addressable (there is an address to find them). All variables are addressable and all constants are unaddressable. We can learn more about addresses and pointers from the article pointers in Go and learn other addressable and unaddressable values from other articles later.

Explicit Conversions On Non-Constant Numeric Values

In Go, two typed values of two different basic types can't be assigned to each other. In other words, the types of the destination and source values in an assignment must be identical if the two values are both basic values. If the type of the source basic value is not same as the type of the destination basic value, then the source value must be explicitly converted to the type of the destination value.

As above has mentioned, non-constant integer values can be converted to strings. Here we introduce two more legal non-constant numeric values related conversion cases.

Unlike constant number conversions, overflows are allowed in non-constant number conversions. And when converting a non-constant floating-point value to an integer, rounding is also allowed. If a non-constant floating-point value doesn't overflow an integer type the fraction part of the floating-point value will be discarded (towards zero) when it is converted to the integer type.

In the following example, the intended implicit conversions at line 4 and line 11 both don't work.

const a = -1.23
var b = a        // the type of b is deduced as float64.
var x = int32(a) // error: constant 1.23 truncated to integer.
var y int32 = b  // error: cannot assign float64 to int32.
var z = int32(b) // okay: z == -1, and the type of z is int32.
                 //       The fraction part of -1.23 is discarded.

const k int16 = 255
var n = k            // the type of n is deduced as int16.
var f = uint8(k + 1) // error: constant 256 overflows uint8.
var g uint8 = n + 1  // error: cannot assign int16 to uint8.
var h = uint8(n + 1) // okay: h equals to 0, and the type of h is uint8.
                     //       (n+1) overflows uint8 and is truncated.

We can think that the type deductions happen at line 2 and line 9 are two implicit conversions, where a and k are both converted to their respective default type. More implicit conversion rules will be introduced in other articles later.

Scopes Of Variables And Named Constants

In Go, we can use a pair of { and } to form a code block. A code block can nest other code blocks. A variable or a named constant declared in an inner code block will shadow the variables and constants declared with the same name in outer code blocks. For examples, the following program declares three distinct variables, all of them are called x. An inner x shadows an outer one.

package main

const y = 789
var x int = 123

func main() {
	// The x variable shadows the package-level variable x.
	var x = true

	// A nested code block.
	{
		x, y := x, y // Here, the left x and y are both
		             // new declared variable. The right
		             // ones are declared in outer blocks.
		// In this code block, the just new declared x and y
		// shadow the outer declared same-name identifiers.
		x, z := !x, y/10 // only z is new declared.
		y /= 100
		println(x, y, z) // false 7 78
	}
	println(x) // true
	println(z) // error: z is undefined.
}

The scope (visibility range in code) of a package-level variable (or a named constant) is the whole package of the variable (or the named constant) is declared in. The scope of a local variable (or a named constant) begins at the end of its declaration and ends at the end of its innermost containing code block. This is why the last line in the main function of the above example doesn't compile.

Code blocks and identifier scopes will be explained in detail in blocks and scopes later.

More About Constant Declarations

The Value Denoted By An Untyped Constant Can Overflow Its Default Type

For example, the following code compile okay.
package main

// 3 untyped named constants
const n = 1 << 64
const r = 'a' + 0x7FFFFFFF
const x = 2e+308

func main() {
	_ = n >> 2
	_ = r - 0x7FFFFFFF
	_ = x / 2
}

But the the following code does't compile.
package main

// 3 typed named constants
const n int = 1 << 64           // constant overflows int
const r rune = 'a' + 0x7FFFFFFF // constant overflows rune
const x float64 = 2e+308        // constant overflows float64

func main() {}

Each Named Constant Identifier Will Be Replaced With Its Bound Literal Value At Compile Time

Constant declarations can be viewed as enhanced #define macros in C. A constant declaration defines a named constant which represents a literal. All the occurrences of a named constant will be replaced with the literal it represents at compile time.

If the two operands of an operator operation are both constants, then the operation will be evaluated at compile time. (Please read the next article common operators for details.)

For example, at compile time, the following code
package main

const X = 3
const Y = X + X
var a = X

func main() {
	b := Y
	println(a, b, X, Y)
}
will be viewed as
package main

var a = 3

func main() {
	b := 6
	println(a, b, 3, 6)
}


The Go 101 project is hosted on both github and gitlab. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.

Support Go 101 by playing Tapir's games. Cryptocurrency donations are also welcome:
Bitcoin: 1xucQbv5jujFPPwhyg395ri5yV71hx9g9
Ethereum: 0x5dc4aa2c2bbfaadae373dadcfca11b3358912212