Golang Constants

In this Golang tutorial, you will learn all about Go constants. Golang constants are similar to variables except for the condition that once a variable is declared as constant it is not possible to make changes to its value. Under this tutorial, you will dive through what are constants in Go, the different syntax used for constant variable declaration, naming conventions, types of constants, and their multiple declarations.

What are Golang Constants?

A Go programming language constant is a variable 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. Go language is read-only and cannot be modified.

How do Go  Constants are declared?

In the Go programming language declaration of constant variables is simple and easy like any other variable declaration as we discussed in earlier tutorials(link to variable declaration).

  •     The keyword const declares a variable in the Go programming language as a constant. 
  •     The keyword const is followed by a variable name and its type to identify the type of value stored by the constant variable.

The syntax for Go Constant

Syntax1


Const  <variable_name> <type> = <value>


const PLACE string = "Europe"
const street_no int = 3456

The syntax declares a constant variable with variable names eg PLACE, street_no, and assigns values to it depending on the type of value the constant variable holds.

PROGRAM 1: Using syntax 1


package main
import "fmt"

const PLACE string = "Europe"
const street_no int =3456

func main() {
 fmt.Println(PLACE)
 fmt.Println(street_no)
}

  • The keyword const makes Europe of type string and the number 3456 of integer type remains unchangeable during the execution of the Go program as constant variables.
  • This syntax is explicit type.

Output:


Europe
3456

Syntax 2:


Const  <variable_name> = <value>

In this syntax, the type of constant variable is inferred during program execution time with no need of predefining the type of variable value contained by the constant variable.Here const keyword declares the variable as constants.This syntax is an implicit type.


const PLACE1 = “Germany”
const street1 = 45

PROGRAM 2: Using Syntax 2


package main
import "fmt"

const PLACE1=”Germany”  //Type inferred
const street1  = 45     //Type inferred

func main() {
 fmt.Println( PLACE1)
 fmt.Println(street1)
}

  • PLACE1 is the constant variable of type string with variable holding “Germany” as its value at its memory location.
  • street1  is another constant variable with integer type holding digits such as 45.

Output:


Germany
45

Rules in Golang constant

  • The naming convention for constant variables is the same as of variables naming rule discussed in the previous tutorial
  • Uppercase letters are mostly preferred to represent a constant variable because of their easiness of identity.
  • Constant variables are declared inside as well as outside of a function.

What are the types of Golang Constants?

Two types of Golang Constants 

  • Typed
  • Untyped
  1. Typed Constants

    Typed Constants are Constant variables that are declared with a specific type. Typed Constants is also known as explicit typing.

    PROGRAM 3:

    
    package main
    import ("fmt")
    /* const declared outside a function */
    const name string = "Typed Constant"
     func main() {
    fmt.Println( name)
    }
    
    
    • A constant variable name of type string declared with value “Typed constant”
    • The constant variable is created with the const keyword.
    • The type of constant variable is mentioned so it falls under the typed constant category.

    Output:

    
    Typed const
    
  2. Untyped Constants

    Untyped Constants are Constant variables declared with unknown types. Untyped Constants is known as implicit typing.

    Program 4

    
    package main
    import ("fmt")
    /* const declared outside a function */
    const z = 100
    
    func main() {
       fmt.Println(z)
    }
    
    

    Output:

    
    100
    
    • A constant variable z is declared using the const keyword with a value of 100.
    • The type of constant is not defined but is identified during program execution./li>
    • So the type of variable in is unknown inferred as integer type only during code compilation and resultant output displayed as 100.
    • So it is known as untyped constants.

Note: Compiler decides the constant variable type in case of untyped constants.

  • Both program 3 & program 4 are examples of constant variables declared outside the function.
  • Consider another example where constant variables are declared inside the function

PROGRAM 5:


package main
import ("fmt")

func main() {
/* const declared inside function */
          const value =10
          const z = 3.14
          const u = "Hi!"

           fmt.Println(value)
           fmt.Println(z)
           fmt.Println(u)
    
    }

How to declare multiple Golang constants?

Multiple constants are grouped into a block. Multiple constant variables are defined using the following syntax

Syntax


Const (
 A = “ one“
 B = 90
 C = 78.90
 D = false 
)

The const keyword is followed with parenthesis “()” and inside the parenthesis defines multiple variables that are assumed to be constants

Program 6:


const (
 PRODUCT  = "Dress "
 QUANTITY = 2
 PRICE    = 1550.50
 STOCK  = true
)
package main
import "fmt"
const (
 PRODUCT  = "Dress "
 QUANTITY = 2
 PRICE    = 1550.50
 STOCK  = true
)
func main() {
 fmt.Println(QUANTITY)
 fmt.Println(PRICE)
 fmt.Println(PRODUCT)
 fmt.Println(STOCK)
}

Output:


2
1550.5
Dress
true

Can constant variables be redeclared?

No, redeclaration of constant variables results in compilation error.

Let us understand with an example


package main
import ("fmt")
func main() {
    const value =10
    const z = 3.14      //  z declared as constant
    const u = "Hi!"
    var z = 3          // z redeclared as variable

    fmt.Println(value)
    fmt.Println(z)
    fmt.Println(u)
    fmt.Println(z)
}

Output:


#_/home/y4zQxY
./prog.go:8:9 z reclared in this book previous declaration at ./prog.go:6:15

Note: Constant variables cannot be redeclared which results in a compilation error