GO Program to print different types of constants

 

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

What are constants in a programming language?

A value that should not be altered by the program during its normal execution is called a constant in a computer programming language.

What are Golang constants?

Go constants are variables whose value remains unchanged. The assigned value remains fixed or unchanged for the constant variable until the successful execution of the Golang program or till the user makes changes to the assigned value of the variable. There are two main types of constants in Go lang. 

  • Typed Constants
  • Untyped Constants

How to display different types of constants in the GO Program

We are using fmt.println() function for printing the string to the output screen. Here we are showing how to print different types of constants in the Go language. The header file math must be import for the mathematical functions. Here str is a string constant and also a,b indicates the constants. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt, math

STEP 2: Initialise string constant str with value "example"

STEP 3: Start function main()

STEP 4: Print string constant str using fmt.println()

STEP 5: Initialise constants a,b

STEP 6: Print d, int64(b), Sin(a) using fmt.println()

 

GO Source Code

                                          package main

import (
    "fmt"
    "math"
)

const str string = "example"

func main() {
    fmt.Println(str)
    const a = 500000000
    const b = 3e20 / a
    fmt.Println(b)
    fmt.Println(int64(b))
    fmt.Println(math.Sin(a))
}
                                      

OUTPUT

example
6e+11
600000000000
-0.28470407323754404