Constants – Golang

In Go, constants are like variables, but their values cannot be changed or reassigned after they are declared. They are immutable, meaning their values remain constant throughout the program’s execution. Constants are useful when you have values that should not be modified to ensure consistency and avoid accidental changes.

In Go, you can declare constants using the const keyword followed by the constant’s name, its data type, and its value:

const constantName dataType = value

Here’s an example demonstrating the use of constants in Go:

package main

import "fmt"

func main() {
    // Declaring and initializing constants
    const pi float64 = 3.14159
    const daysInWeek int = 7

    // Using constants in expressions
    const radius float64 = 5.0
    area := pi * radius * radius

    // Constants with inferred types
    const appName = "MyApp"
    const version = "1.0.0"

    // Printing constants
    fmt.Println("Pi:", pi)
    fmt.Println("Days in a Week:", daysInWeek)
    fmt.Println("Area of a Circle with radius", radius, ":", area)
    fmt.Println("App Name:", appName)
    fmt.Println("Version:", version)

In this example:

  1. We declare and initialize two constants, pi (with value 3.14159) and daysInWeek (with value 7).
  2. We use constants in an expression to calculate the area of a circle with a given radius.
  3. We declare two constants, appName and version, without explicitly specifying their data types. Go automatically infers the data type based on the assigned values.
  4. We print the values of the constants using fmt.Println().

When you run this Go program, it will output:

Pi: 3.14159
Days in a Week: 7
Area of a Circle with radius 5 : 78.53975
App Name: MyApp
Version: 1.0.0

As you can see, constants provide a way to define fixed values in your Go programs, ensuring that these values remain unchanged throughout the program’s execution. They are a useful tool for maintaining consistency and clarity in your code.

Untyped and Typed Numeric Constants: 

In Go, constants can be categorized into two types based on their data type: untyped and typed constants. This distinction is important because it affects how these constants can be used and interact with other values in Go expressions.

  1. Untyped Constants:
    • 1). Untyped constants are constants that do not have a specific data type associated with them during declaration.
    • 2). The Go compiler automatically infers the data type of untyped constants based on their context and usage.
    • 3). Untyped constants can be used in expressions and are subject to implicit type conversions if needed.

Example of an untyped constant:

package main

import "fmt"

func main() {
    const untypedConstant = 42 // Untyped constant with no specified data type

    // Untyped constant used in expressions
    result := untypedConstant + 10
    fmt.Println("Result:", result)

    // Untyped constant used with explicit type conversion
    radius := 5.0
    area := 3.14159 * radius * radius
    fmt.Println("Area of Circle:", area)

In the example above, untypedConstant is an untyped constant with a value of 42. It is used in an expression where untypedConstant + 10 involves an addition operation with an integer literal 10. The result is implicitly converted to the same type as untypedConstant, resulting in an untyped integer constant 52.

  1. Typed Constants:
    • 1). Typed constants have a specific data type explicitly specified during their declaration.
    • 2). The type of the constant remains fixed and cannot be implicitly converted to other types.
    • 3). Typed constants are useful when you want to ensure a constant’s data type is well-defined and consistent.

Example of a typed constant:

package main

import "fmt"

func main() {
    const typedConstant int = 42 // Typed constant with data type 'int'

    // Typed constant used in expressions
    result := typedConstant + 10
    fmt.Println("Result:", result)

    // The following line will cause a compilation error
    //typedConstant = 100 // Error: cannot assign to typed constant 'typedConstant'

In the example above, typedConstant is a typed constant with a value of 42 and a specified data type of int. When used in an expression with typedConstant + 10, it can only be added to an int value, and the result is also of type int.

To summarize, untyped constants are more flexible and can be used in various contexts without explicit type specification. On the other hand, typed constants provide explicit type information and are useful when you need to enforce a specific data type for the constant’s value. Understanding the distinction between untyped and typed constants is essential for working with constants effectively in Go.