GoLang Variable Declaration

Golang Variable: A variable is just a name given to a particular memory space in the storage, by which the Go program manipulates that variable. The memory space in the go program is decided by the type of the variable, and each type has its pre-decided size in the go language. The type of variable decides the storage and the range of the value in the go program.

A variable name consists of alphabets, digits and underscore, which represent data stored in the storage of your computer. At the time of the declaration, the compiler creates a memory block for the variable in the storage. The compiler uses the variable name for accessing that data during the execution of the go program.

Rules for creating a variable:

There are several rules for creating a variable in the go program. A few of them are given below:

Rule no. 1:

A variable name must start with an alphabet or an underscore (_). The alphabets can be in lower case (a to z) or upper case (A to Z).

Rule no. 2:

In the go language, it becomes necessary to use the variable if you declared it inside the main function in your program.

Rule no. 3:

The developer can define the new values to the pre-defined variable, but it is just that they should be the same type.

Rule no. 4:

If a variable is defined inside a curly brace, then the variable's scope will be inside the curly brackets.

Rule no. 5:

Since Go language is a statically typed language, so it becomes necessary to assign a type to the declared variable in the go program. It can be either an explicit type assignment or an implicit assign.

Rule no. 6:

A variable name can't start with a digit (0 to 9).

Rule no. 7:

Since the Go language is a case sensitive language, so the variable declared in the upper case and in the lower case will be different.

Declaration of a variable:

In the go language, we use the var keyword for the declaration of a variable. You can also define a variable with or without assigning it a value in the program.

Syntax:

The syntax for the variable declaration in the go language is:

var variable_name variable_type = value

Here, var is a keyword, variable_name is an identifier, variable_type is the type of the variable, and value is that which will be assigned to the variable.

Formats of declaration:

There are multiple formats for defining a variable in the go program. These formats are:

  1. Normal variable declaration
  2. Shorthand variable declaration
  3. Multiple variable declarations
  4. Block declaration
Normal variable declaration:

It is also known as the static type declaration. In this format, the variables are defined normally in the go program. It uses the var keyword for the declaration. You can define a variable anywhere in the program by using this format.

It also includes two ways for variable declaration, i.e., with data type and without data type.

  1. With data type: It is a formal way of the variable declaration. This declaration format is important when the developer wants to provide the value to the variable somewhere else in the program, not at the time of declaration. It is useful when the developer takes the input from the end-user. The syntax is:
var variable_name variable_type = value

                 Here, the value is optional.

  1. Without data type: In this format, the type declaration is optional. The compiler considers the type of the variable based on its value. In this declaration, the value is important. The syntax is:
Var variable_name = value

Program:

Here is a program with a normal variable declaration given below:

 package main
 // normal variable declaration
 import "fmt"
 func main() {
             // With data type
             var num1 int = 10
             var name1 string = "Govind"
             // Without data type
             var num2 = 20
             var name2 = "Radhika"
             fmt.Println(num1, num2)
             fmt.Println(name1, name2)
 } 

Output:

GoLang Variable Declaration

Shorthand variable declaration:

It is another format for defining a variable in the go program. In this format, you don't require a var keyword for declaration, even the type of value is also not required. You can define a variable just by using its name, value and an operator (:=). The compiler interprets the type of the variable based on its value. In shorthand declaration, we use a colon followed by an equal operator, i.e., ':=' for assigning value to the variable.

In this format, it is mandatory to provide the variable value at the time of declaration.

It is also known as a dynamic type declaration because you can declare the variable easily anytime and anywhere in the go program.

Syntax:

The syntax of short-hand variable declaration is:

            <variable_name> := <value>

Example:

num := 10

Here, ‘num’ is a variable, and its value is 10. Because 10 is an integer type value, so the type of variable ‘num’ will be ‘int’.

>>> You can also declare more than one variable in a single statement just like:

            a, b, c := 10, 20, 30

>>> You can also declare mixed type variable in a single statement just like:

            a, b, c, d := 10, 10.5, “abc”, true

Program of shorthand declaration of a variable:

Here is a program of shorthand declaration of a variable given below. This program includes all the above concepts of shorthand declaration.

 package main
 // sort hand variable declaration
 import "fmt"
 func main() {
             num1 := 10
             num2 := 10.5
             num3 := true
             name1 := "Govind"
             name2 := "Radhika"                         // variable declared
             fmt.Println(num1, num2)
             fmt.Println(num3)
             fmt.Println(name1, name2)
             fmt.Printf("\nThe type of num1 is: %T \n", num1)
             fmt.Printf("The type of num2 is: %T \n", num2)
             fmt.Printf("The type of num3 is: %T \n", num3)
             fmt.Printf("The type of name1 is: %T \n", name1)
             fmt.Printf("The type of name2 is: %T \n", name2)
 } 

Output:

GoLang Variable Declaration

Multiple variable declarations:

The go language allows the developer to declare multiple variables in a single statement of the go program. The developer can also define different types of variables in a single statement; that's why this format is also known as mixed variable declaration.

You can define the variable in this format with the help of both the above formats, i.e., normal format and shorthand format. But in normal format, you can declare multiple variables only in the second way, i.e., without data type. You can't declare multiple variables if you are

using the normal format with data type in the go program.

For example:

             var a, b, c = 10, 20.5, “abc”             // Valid syntax
             a, b, c := 10, 20.5, “abc”                   // Valid syntax
             var a, b, c int, float64, string              // Invalid syntax
             var a, b, c = int, float64, string         // Invalid syntax 

Program for multiple/mixed variable declarations:

 package main
 // multiple variable declaration
 import "fmt"
 func main() {
             var num1, num2, name = 10, 10.5, "Govind"
             a, b, c := 11, 11.23, "Radhika"            // Variable declaration complete
             fmt.Println(num1, num2, name)
             fmt.Println(a, b, c)
 } 

Output:

GoLang Variable Declaration
Block declaration:

 It is another format for defining a variable in the go program. It is also a useful format for the developer while working on a large-sized project. In this format, the var keyword is followed by a pair of parenthesis, and all the variables will be declared inside this parenthesis. You can define a variable with the data type or without data type in this format. The developer can use with data type option for a variable declaration if he doesn't want to give its value at the time of declaration.

The developer can also declare multiple and mixed values in a single statement.

Syntax:

The syntax of declaring variable in block declaration format is given below:

 var (
             variable_name = value
             variable_name variable_type
 ) 

Example:

 Var (
             a = 10
             a, b, c = 10, 10.5, “abc”
             c int
             d string
 ) 

Program for block declaration:

Here a program is given below in which we are going to use a block declaration format for defining a variable.

 package main
 import "fmt"
 func main(){
             var (
                         num1 = 10
                         num2 = 10.5
                         name1, name2 = "Govind", "Radhika"
                         a int
                         b float64
                         c string
             )
             /* a = 10
             b = 11.25       
             c = "abc" */
             a, b, c = 10, 11.25, "abc"                 // assigning value in single line.
             fmt.Println(num1, num2)
             fmt.Println(name1, name2)
             fmt.Println(a, b, c)
 } 

Output:

GoLang Variable Declaration

Difference between normal and shorthand declaration:

These are two ways of declaring a variable in the go program. The difference between a normal variable declaration and a short variable declaration is given below:

Normal Variable Declaration  Shorthand Variable Declaration
It requires keywords in the declaration of a variable.It doesn't require keywords in the declaration of a variable.
The data type can be defined.Data type can't be defined.
The compiler automatically assign the type of variable if it is not provided in the syntax, which means data type declaration of variable in this format is optional. There is no data type declaration for the variable; it is automatically assigned by the compiler according to the value.
The developer can do the declaration and initialization of the variable separately.The declaration and the initialization will be made at the same time.
Variables can be declared both inside and outside of the functions.Variables can be declared only inside the functions.
It is not mandatory to provide the value to the variable at the time of declaration.It is mandatory to provide the value to the variable at the time of declaration.
Var keyword is used.No keyword uses.
Equal operator '=' is used in the declaration.Short declaration operator ':=' is used in the declaration.
 The variables declared with this format have both local and global scope.The variables declared with this format have only local scope.

The default value of the go variable:

If the variable is defined without an explicit initial value, then the value given to that variable is zero.

In simple words, if you define a variable in the go program without assigning it a value, then the variable will contains the default value. This default value will be assigned according to their types.

The value of variables based on their types is given below:

For numeric (integer and float) type variable: 0 (zero)

For Boolean type variable: false

For string type variable: " " (a blank space).

Example:

var age int

var name string

var marks float32

var pass bool

Program:

Here a program is given below in which we will get the default value of the declared variable.

 package main
 import "fmt"
 func main() {
             var name string
             var age int
             var marks float32
             var pass bool
             fmt.Println("Here, the default value of all declared varibles is:")
             fmt.Println(name)                             //  it gives blank space.
             fmt.Println(age)                                //  it gives zero.
             fmt.Println(marks)                            //  it gives zero.
             fmt.Println(pass)                               //  it gives false.
 } 

Output:

GoLang Variable Declaration

lvalue and rvalue in the variable declaration:

lvalue and rvlaue are defined in the expression of variable declaration, or you can say these are two parts of the variable declaration expression.

lvalue:

The lvalue is the left part of the variable declaration expression. It is the name of the variable which the developer gives in the go program.

The part, which refers to the storage location, is known as lvalue in the go program.

rvalue:

The rvalue is the right part of the variable declaration expression. It is the data value, which is stored in the memory location that the lvalue indicates.

The part that declared the value of the variable in the expression is known as the rvalue in the go program.

            The moral of the debate is; variables are the lvalues, and their values are the rvalues in the declaration expression of the go program. For example:

            num := 10

OR

            var num int = 10

Here, the variable name 'num' is the lvalue and its value '10' is rvalue in this expression.

But if the developer is declaring a variable with data type and not assigning any value to it, for example:

var num int

Although it is a valid declaration, in the case of rvalue and lvalue, only one part of the expression is available here, so the variable 'num' is an lvalue, and it will get its rvalue when the developer is assigned (or get itself) a value to this variable in the go program.



ADVERTISEMENT
ADVERTISEMENT