Variables in Go lang

What is variable

Variable is an entity which can change during the problem.

For example:

y = x + 5

x y
1 6
2 7
3 8

As shown in the above example, as the value of x is changing y gets changed. so in the above expression x and y are variable and 5 is a constant.

In programming whenever you create a variable, a memory is reserved and a name is assigned to it. If initialized with value then binary value of that will gets stored in memory otherwise initialized with default value. Following figure shows the memory map of a variable.

Creating a variable

var name string = "Hello Go"

  • In the above example, a variable is created labeled with the name of type string and initialized with value "Hello Go". the var keyword is used to create a variable.
var name = "Hello Go"

  •  In the above code snippet, the only difference is that data type is not given, because when you initialize the variable then compiler automatically adopts the type from the initialized value.
name := "Hello Go"

  • Another way to create a variable is a short variable declaration, the short variable declaration is done by using := operator. In the short assignment, the type is automatically adopted from its value and it is declared also. 

Example-1:

package main
import "fmt"
func main() {
var name1 string = "Alex" //variable declaration
fmt.Println(name1)
var name2 = "Clark" // optional type
fmt.Println(name2)
name3 := "Mahesh" //short variable declaration
fmt.Println(name3)
}

Example-2:

package main
import "fmt"
func main() {
name := "Sunny"
age := 24
fmt.Println(name,"is",age,"years old")

Output: 

Sunny is 24 years old

Example-3:

package main
import "fmt"
func main() {
name := "Sunny"
age := 24
fmt.Println(name+"is"+age+"years old")
}
Compile time error: invalid operation: (name + "is") + age (mismatched types string and int)

Example-4 

package main
import (
      "fmt"
      "strconv"
)
func main() {
name := "Sunny"
age := 24
fmt.Println(name+" is "+strconv.Itoa(age)+" years old")
}

 Output

  1. To remove the error shown in the Example-3, you need to convert age into the string, because In Go lang string can only be concatenated with a string.
  2. To convert age into a string, use Itoa() function from the strconv package, which converts Integer to string, other function Atoi() converts a string to an integer.
  3. Because we use the strconv package you need to import this package also.
  4. To import multiple packages you can use the following syntax.
import "fmt"
import "strconv"
  • In the above snippet, two package fmt and strconv are imported each in a separate line. 
import (
        "fmt"
        "strconv"
)

  • In the above program snippet, multiple packages are grouped together by using brackets ( ), but each package is written in the separate line.
import ("fmt";"strconv")

  • Above program snippet import multiple packages in a single line, in this case, each package name must be separated by a semicolon ; 



Comments

Popular posts from this blog

String in golang

Arrays in C Language

Literals in Java

Pointers in C Language

Inline V/S Block Level Element

Reserved Words in Java

Identifiers

Data Types in Go language

Printing in C programming

Variable Naming & Scope of Variable