Trending October 2023 # How To Declare Variables In Go # Suggested November 2023 # Top 16 Popular | Saigonspaclinic.com

# Trending October 2023 # How To Declare Variables In Go # Suggested November 2023 # Top 16 Popular

You are reading the article How To Declare Variables In Go updated in October 2023 on the website Saigonspaclinic.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How To Declare Variables In Go

Definition of Go Variables

Variables in go language is an important attribute; they used to hold the value for any numeric, string, or boolean value. Basically, if we have some number, then we can assign that number to any variable and later we can access that variable with the name of the variables, it happens because of every variable in go language assigned with a memory address, and when we try to access that value with variable name actually we are accessing that value with its memory address where it stored because we can not remember the memory address of any value we give them some user-friendly name and we call them variables.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How to Declare Variables in Go?

var studentName;

How to Initialize Variables in Go?

Below is a simple syntax to initialize any variable; we just took an example with a student name and assigned it a name. We have used the keyword variable before the name of the variable and assign the value to the variable.

var sudentName =”Ranjan”

Types of Variables in Go

Broadly we can define three types of variables they are:

Numeric Variables: These types of variables contain numeric values; these numeric values can be either integer or decimal numbers. They can be negative numbers also. So all the number related variables come under it

String Variables: These are the variables that hold the address for the string-like name, address, etc.

Boolean Variables: These are the variables that hold true or false, and they are used to check some flag like yes or no.

First, we are going to discuss the numeric variables(integer and floating)

1. Integer Variables

Integer variables are used to hold the integer non-decimal value. It holds the memory address of any integer value by which we can access it.

Once we define the variable as the integer, we inform the compiler about how the compiler will need much memory space to hold the variable.

We can perform the all basic operation of comparison and addition on the integer type variables.

We can use the + operator for addition, and we can use the == operator for comparing two variable values.

Note: Always comparison means comparing the value of the variables, not the memory address held by the variable. Because memory address is always different for every variable.

Example

Here in the below example, we perform addition and comparisons and print the output of the two variables  price1 and price2. The comparison will print the output as the boolean value true and false.

Code:

}

Output:

2. Floating Type Variables

The floating type variables are the variable that holds the variable of value with decimal numbers.

Once we define the variable with a decimal number, the go compiler will reserve the required space for any floating value.

Examples of floating-point values are like 5.5,4554.7 etc.

Example

Here in the below example, we perform addition and comparisons and print the output of the two floating variables price1 and price2. A comparison of two floating variables will print the output as the boolean value true and false.

}

Output:

3. String Variables

String type variables are the variables that hold the string only.

We can create string variables in the language in two ways, either by using a single quote(‘’) or by using the double quote(“).

Mostly string variables are used for holding string type value like name, address, etc.

Example

In the below example, we are using two string variables, student1 and student2, and these two variables are holding the name of two students. We are performing two operations here one, we are adding these two variables, and second, we are comparing these two variables.

Code:

}

Output:

4. Boolean Variables

Boolean variables are the variables that hold only two things, true and false.

Once we define a variable as the boolean, our variable as the boolean, the go compiler will allocate the memory location for true and false.

We use the boolean variables where we wanted to make some flags for true and false only.

Example

Code:

}

Output:

Rules and Regulation for Declaring Variables in Go

There are a few important rules for declaring any variable in go language; they are given below.

We can not declare any variable of two types, like in the below.

var X.

This will give an error.

We can not declare or change the type of variable; if we declare a variable as the integer and again if we declared it as a string, then it will throw an error.

We can not initialize any value before declaring the variable in go language.

Conclusion

From this tutorial, we learned some of the important concepts about the variables in go language; we learned declaration and initialization, focused on the types of the variables with some examples and learned about the various important rules used for variable declaration.

Recommended Articles

This is a guide to Go Variables. Here we also discuss the definition and how to declare variables in go along with different examples and its code implementation. You may also have a look at the following articles to learn more –

You're reading How To Declare Variables In Go

Update the detailed information about How To Declare Variables In Go on the Saigonspaclinic.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!