A variable is a user defined name which holds a value during program execution. This value is stored in memory and variable is used to refer to it.
This value can be fetched or updated(modified) using the variable name.
variable declaration in goAbove illustration contains a variable “name” which points to a memory location. Value at this memory location can be fetched or modified only using this variable.

Defining variables in go
Variables in go are declared using var keyword followed by the name of variable and then its type as shown below.

// declare a string variable
var name string
// declare an int variable
var count int

You can also declare multiple variables in the same statement if they are of the same data type separated by a comma.

// declare multiple string variables
var name, section, code string

//declare multiple int variables
var counter, class, id int

Initializing variables
Initializing means providing a value to variables. Variables are initialized using = with the variable at the left hand side and the value at the right side of =.
Variables can be initialized in the same statement in which they are declared or in a separate statement after they are declared. Example,

// declaration
var site string
// initialization
site = "codippa.com"

Variables can be initialized in the same statement as

// declaration and initialization
var site string = "codippa.com"

This is called Static variable declaration since the type of variables is defined at the time they are declared. It is also possible to declare and initialize variables without giving their types. This is called Dynamic type declaration and is discussed next.

Dynamic Type declaration
Variables in go can be declared without specifying their types. Go automatically infers their types from the value given to them while initialization.
There are two ways to define the type of variable dynamically.
First is to declare and initialize the variable in the same statement. In this case, you can omit the type of variable. Go will determine its type from the value given. Example,

// dynamic type declaration
var site = "codippa.com"

In this case, Go will understand that the variable is of type string. Note that after this statement, you can not change the type of variable.
Thus, if you try to change its value to 20, you will be served with an error

cannot use 20 (type int) as type string in assignment

Multiple variables can also be initialized using this syntax with the declaration and initialization both separated by comma. Example,

// multiple variables dynamic declaration
var site, age, rate = "codippa.com", 24, 12.34

With this syntax, the number of variables and the count of values assigned to them should match else there will be an error.

assignment mismatch: 2 variables but 1 values

Second way to define the type of a variable dynamically is using := operator which is a colon followed by =.
This is a shorthand notation of creating a variable since it does not even require using var keyword. Example,

//dynamically declare and initialize string variable
site := "codippa.com"

Above variable declaration is perfectly legal. Again, Go will determine the type of variable from the value assigned.

Leave a Reply