Go Constants

A constant is a value that cannot be changed during program execution. Constants are useful for defining values that should not be modified, such as mathematical constants or configuration settings. Constants are declared using the const keyword and must be assigned a value at the time of declaration.

Here’s an example of how to declare and use constants in Golang:

package main

import "fmt"

func main() {
    // Declare a constant called "pi" with a value of 3.14159
    const pi = 3.14159

    // Declare a constant called "name" with a value of "John"
    const name string = "John"

    // Declare multiple constants at once
    const (
        x = 1
        y = 2
        z = 3
    )

    // Print the values of the constants
    fmt.Println("Pi:", pi)
    fmt.Println("Name:", name)
    fmt.Println("x:", x, "y:", y, "z:", z)
}

In this example, we declare a constant called “pi” with a value of 3.14159 and a constant called “name” with a value of “John”. We also declare multiple constants at once using the const keyword.

Finally, we print the values of the constants using the fmt.Println function. When you run this program, you should see the following output:

Pi: 3.14159
Name: John
x: 1 y: 2 z: 3
Follow us on social media
Follow Author