Updated April 13, 2023
Introduction to Golang Constants
Constants in go are the variable that once assigned the value and initialize we cannot change it during the process of the running of the program life cycle. The main importance of using the constant in go language allows us to define a variable which will be fixed throughout the compilation of the program. There are broadly three types of constant in the go language they are string constants (containing string value), numeric constants (containing an integer and floating values), Boolean constant (containing true and false value). In general programming, the constants are defined at the top of the program and will be available for the whole flow of the code and its value will be the same for the cycle. In this topic, we are going to learn about Golang Constants.
List of Constants in Golang
Broadly we can define three types of constants in go languages, we have numeric constants (Any numeric constant can be defined in two types one is an integer and another one is floating), string constants (string constants are the constant which contains the string value separated by either single or double quote), and another one is the boolean type constant (boolean types are the type which holds the true or false),.
1. Integer Constant
- Integer type constants are the constant which contains the integer value.
- In any integer type constant, they contain an integer part.
- We can use the == operator for comparing two integer type constants.
- We can use the = operator for assignment of any integer type constant.
Example for an integer constant
In the below example we have taken two variables X and Y and these variables contain two integer numbers. Here we are performing two operations inside the example, one we are adding them printing the resultant output of them, second we are comparing these two variables and printing the output of comparison of these two integer values.
Please see the below example along with the screen of output
Code:
package main
import "fmt"
func main() {
const X= 3 //Defining const variable
var Y = 6 //Defining var variable
// Adding these two integer together
var sum = X+Y
fmt.Println(sum)//printing the sum of the two numbers
// Here we are comparing these numbers
fmt.Println(X == 3)
fmt.Println(Y < X)
}
Output:
2. Floating Type Constant
- Floating type constants are the constant which contains the decimal value integer.
- In any floating type constant they contain two parts one is the faction part value and another is the exponent part value.
- We can use the == operator for comparing two floating type constants.
- We can use the = operator for assignment of any floating type constant.
- At the time of displaying the floating type constant we need to consider the decimal points.
Example of Floating Type constant
In the below example we have taken two variables X and Y and these variables contain two floating numbers. Here we are performing two operations inside the example, one we are adding them printing the resultant output of them, second we are comparing these two variables and printing the output of comparison of these two floating values.
Please see the below example along with the screen of output
Code:
package main
import "fmt"
func main() {
const X= 3.4
var Y = 6.9
// Adding these two floating type constants together
var sum = X+Y
fmt.Println(sum) //printing the sum of the two numbers
// Here we are comparing these floating type constants
fmt.Println(X == 3.2)//It will print true or false
fmt.Println(Y < X)
}
Output:
3. String Constants
- In Go language we can use two types of the constants one is with the single quote(‘’) and another with the double quote(“”).
- We can perform all the operations on the string like string concatenation, to perform string concatenation in the language we can use + symbol. We can also use a format like +=.
- Many times you will see the requirement of comparing the two strings in go language, so we can use the == operator for comparison of string.
- We can simply use the = operator for operating on assignment.
Example for String Type constant
In the below example we have taken two variables X and Y and these variables contain two string values. Here we are performing two operations inside the example, one we concatenate them printing the resultant output of them, second, we are comparing these two variables and printing the output of comparison of these two strings values.
Please see the below example along with the screen of output
Code:
package main
import "fmt"
func main() {
const X= "Ranjan"
var Y = "Ajay"
// concatenate these two string together
var greetings = X+ " " + Y //Output will produce two strings combined together .
greetings += "!"
fmt.Println(greetings)
// Here we are comparing these strings
fmt.Println(X == "Ranjan") //It will print true or false
fmt.Println(Y < X)
}
Output:
4. Boolean constant
Booleans are a very common type of constant defined in the go language, we can explain the properties of the boolean constants in the following steps.
- The boolean constants are very much similar to any string constants.
- The main difference between string constants and boolean constants is they allow us to define two types true and false whereas in case of the string constant they are only string.
- Boolean constants are used for managing some flag values like yes or no or true or false.
Example for Boolean Type constant
In the below example we are performing some operations on a boolean value, we are assigning and printing the boolean value in the go language.
Please see the below example along with the screen of output
Code:
package main
import "fmt"
const Py = 3.14
func main() {
const boolConst = true
type exampleBool bool
var initialBool = boolConst // allowed
var simpleBool exampleBool = boolConst // This way it will work
// simpleBool = initialBool //This way it will not work
fmt.Println(initialBool) //Printing the intialBool value
fmt.Println(simpleBool) //printing the simpleBool value
}
Output:
Conclusion
From this tutorial we learned about the basic concept of the constant in the go language and we learned about its various types. We learned with the help of the various examples of the various types of constants in the go language.
Recommended Articles
This is a guide to Golang Constants. Here we discuss the List of Constants in Golang with programming examples for better understanding. You may also have a look at the following articles to learn more –