Module: Go Basics

Constants

Go Programming: Constants

Constants in Go are fixed values that cannot be changed during program execution. They are useful for representing values that are known at compile time and should not be modified.

Declaring Constants

Constants are declared using the const keyword. Here's the basic syntax:

const identifier type = value
  • const: Keyword indicating a constant declaration.
  • identifier: The name of the constant. Follows the same naming rules as variables.
  • type: The data type of the constant. Go can often infer the type, but it's good practice to explicitly specify it.
  • value: The constant value. Must be a valid expression that can be evaluated at compile time.

Examples:

package main

import "fmt"

func main() {
	const pi float64 = 3.14159
	const greeting string = "Hello, world!"
	const answer int = 42

	fmt.Println(pi)
	fmt.Println(greeting)
	fmt.Println(answer)
}

Output:

3.14159
Hello, world!
42

Constant Inference

Go can often infer the type of a constant based on the value assigned to it. This simplifies the declaration:

package main

import "fmt"

func main() {
	const pi = 3.14159 // Type is inferred as float64
	const greeting = "Hello, world!" // Type is inferred as string
	const answer = 42 // Type is inferred as int

	fmt.Println(pi)
	fmt.Println(greeting)
	fmt.Println(answer)
}

This produces the same output as the previous example.

Multiple Constant Declarations

You can declare multiple constants in a single const block:

package main

import "fmt"

func main() {
	const (
		width  int = 10
		height int = 20
		depth  int = 5
	)

	fmt.Println("Width:", width)
	fmt.Println("Height:", height)
	fmt.Println("Depth:", depth)
}

Output:

Width: 10
Height: 20
Depth: 5

If you omit the type in a multiple constant declaration, Go will use the type of the first constant in the block for all subsequent constants if their values are compatible.

package main

import "fmt"

func main() {
	const (
		a = 10
		b = 20
		c = 30
	)

	fmt.Println(a)
	fmt.Println(b)
	fmt.Println(c)
}

Output:

10
20
30

In this case, b and c are also inferred to be of type int.

Untyped Constants

Go supports untyped constants. These constants don't have a specific type assigned to them. Their type is determined by how they are used.

package main

import "fmt"

func main() {
	const zero = 0 // Untyped constant

	var i int = zero // zero is treated as int here
	var f float64 = zero // zero is treated as float64 here

	fmt.Println(i)
	fmt.Println(f)
}

Output:

0
0

Untyped constants are particularly useful for defining symbolic constants that can be used with different types.

Constant Expressions

Constant expressions are expressions that can be evaluated at compile time. They can involve constants, literals, and basic arithmetic operations.

package main

import "fmt"

func main() {
	const radius = 5
	const area = 3.14159 * radius * radius

	fmt.Println("Area:", area)
}

Output:

Area: 78.53975

Limitations of Constants

  • Cannot be modified: Once a constant is declared, its value cannot be changed.
  • Must be known at compile time: The value of a constant must be known at compile time. You cannot use a variable or a function call to initialize a constant.
  • Limited operations: Constants can only be involved in operations that can be evaluated at compile time. For example, you cannot use a constant to index an array if the index is not known at compile time.

When to Use Constants

  • Representing fixed values: Use constants for values that are known and should not change, such as mathematical constants (pi, e), configuration settings, or error codes.
  • Improving code readability: Constants can make your code more readable by giving meaningful names to fixed values.
  • Enhancing maintainability: If a fixed value needs to be changed, you only need to modify it in one place (the constant declaration) instead of searching throughout your code.
  • Compiler optimizations: The compiler can often optimize code that uses constants, leading to improved performance.