Constants in Go



In this tutorial, we will learn about constants in Go. We will cover the basics of defining and using constants, including examples of different constant types and their applications.


What is a Constant

A constant in Go is a value that cannot be changed once it has been assigned. Constants are defined using the const keyword and can be of any basic data type.


Defining Constants

Constants can be defined in Go using the const keyword followed by the name of the constant and its value:

const pi = 3.14

Constants can also be defined with explicit type:

const greeting string = "Hello, World!"


Example 1: Basic Constant Definition

We can define a basic constant in Go without specifying its type.

For example,

  1. Define a constant without specifying the type.
  2. Print the value of the constant.

Go Program

package main
import "fmt"
func main() {
    const pi = 3.14
    fmt.Println(pi)
}

Output

3.14


Example 2: Constant with Explicit Type

We can define a constant with an explicit type in Go.

For example,

  1. Define a constant with an explicit type.
  2. Print the value of the constant.

Go Program

package main
import "fmt"
func main() {
    const greeting string = "Hello, World!"
    fmt.Println(greeting)
}

Output

Hello, World!


Example 3: Using Constants in Conditional Statements

We can use a constant in an if-else statement to check if a number is positive, negative, or zero.

For example,

  1. Define a constant for an integer value.
  2. Use an if statement to check if the constant is positive, negative, or zero.
  3. Print a message based on the condition.

Go Program

package main
import "fmt"
func main() {
    const number = -10
    if number > 0 {
        fmt.Println("The number is positive.")
    } else if number < 0 {
        fmt.Println("The number is negative.")
    } else {
        fmt.Println("The number is zero.")
    }
}

Output

The number is negative.


Example 4: Checking if String Constant Contains a Specific Substring

We can check if a string constant contains a specific substring in Go.

For example,

  1. Define a constant for a string value.
  2. Use an if statement to check if the string contains a specific substring.
  3. Print a message based on the condition.

Go Program

package main
import (
    "fmt"
    "strings"
)
func main() {
    const phrase = "Go is awesome"
    if strings.Contains(phrase, "awesome") {
        fmt.Println("The phrase contains 'awesome'.")
    } else {
        fmt.Println("The phrase does not contain 'awesome'.")
    }
}

Output

The phrase contains 'awesome'.


Example 5: Using Constants in a Loop

We can use a constant to define the number of iterations in a loop.

For example,

  1. Define a constant for the number of iterations.
  2. Use a for loop to iterate a constant number of times.
  3. Print the loop counter in each iteration.

Go Program

package main
import "fmt"
func main() {
    const iterations = 5
    for i := 1; i <= iterations; i++ {
        fmt.Println("Iteration", i)
    }
}

Output

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5