What Is A Constant Variable?

Definition

A constant variable is a variable that once defined cannot be changed throughout the execution of the source code.

Use Cases and Examples


01: package main
02: 
03: import "fmt"
04: 
05: const Pi = 3.14
06: 
07: func main() {
08:     const currencySymbol = "$"
09:     fmt.Println("Currency: ", currencySymbol)
10: }

In the Go code above we set the constant variable currencySymbol on line 05 to "$". In this case, we are assuming the dollar symbol will be used within the entire application without needing to be changed to something else. To better frame it, by setting it to a constant we ensure this is never changed within the application down the line. This is very helpful if you have multiple people working on the same codebase.

Summary

Use a constant variable if you want to ensure the variable value is not changed within the code sometime down the line.

Here is another article for you 😊 "what is hashing?"