Recent Posts
- jQuery attrAugust 12, 2021
- jQuery mouseenterAugust 9, 2021
- jQuery ToggleclassAugust 6, 2021
- jQuery attr
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.
Broadly golang can be 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),.
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)
}
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
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)
}
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 golang example along with the screen of output
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)
}
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.
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
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
}
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.
Learn more about programming in mcp
MCP is the right place for best computer Courses institute and advance Courses in Mohali and Chandigarh.The Complete Programming Academy can change your life – providing you with the knowledge, skills, and performance in a second language which helps you to excel in your job.You can also Contact us for 6 month industrial training institute in Mohali.