Explain Go Variables in Go Programming Language | Go Programming Tutorial
In this article, you will learn
- What are variables?
- Rules for Naming Variables
- How to declare variables?
What are variables?
A typical program uses various values that might change during its execution. For example, a program that performs some operation on the values entered by the user. The values entered by one user might differ from the values entered by different users. Hence, it is important to use the concept of variables so different users can enter different values. When a user enters a different value it will be processed and can store in the Random Access Memory of the computer temporarily. Throughout the execution of the program, these values will vary, and hence it is known as variables. In simple terms, it is information that varies throughout the execution of the program. The variables give the right to retrieve and manipulate the stored information.
Rules for Naming Variables:
- The name of the variable must begin with a letter or an underscore(_). It can have letters and numbers as well in the name.
- It can not start with a numeric value.
- The name of the variable should be case-sensitive.
- You are not allowed to use the keyword as the variable name.
- The length of the keyword can be any letter.
Declaring a Variable
1. Using var Keyword: The name of the variable is created using a var keyword of a particular type, which is connected with the name and provides its initial value.
var name_of_the_variable type = expression
Points to Remember:
- The type or = expression can be excluded but not both.
- If the = expression is excluded, then the variable value is taken as its default value. The default value is usually 0.
- If the type is excluded, then the type of the variable is assumed by the initial value of the expression.
- The expression is excluded, then the variable has zero values for the type like zero for the numbers, false for booleans, “for strings, and nil for the reference and interface type. Hence, there is no concept of uninitialized variables.
2. Using the short variable declaration: The local variables which are declared and initialized in the functions in the Go programming language are declared by using the short variable declaration.
Name_of_the_variable := expression
Don’t consider := and =as the same, they both are different as := is a declaration and = is an assignment.


