Variables in GO Tutorial

Udemy Generic 728x90

Definition

Like in any other language variable in Golang are the storage locations which holds some known or unknown quantities or information. Variable has an associated name which we we call as identifiers. It is the symbolic name of the variable.

Rather than sending some string literal or value directly to a function or program or in part of a function we first assign this value to a variable and send it to the function or program or in part of function.

Defining a variable

A variable in Golang is defined using var keyword followed by variable name and type of variable .So anything you found started with var key word followed by a name and type is a variable.

Var x string
var x = “I am Golang variable”

In the above example we see the = symbol we have a tendency to read that as “x equals the string I am Golang variable”. There’s nothing wrong with reading our program that way, but it’s better to read it as “x takes the string I am Golang variable” or “x is assigned the string I am Golang variable”. This distinction is important because (as their name would suggest) variables can change their value throughout the lifetime of a program.

Name a variable

Name of the variable may contains any letter number or underscore.Go compiler doesn’t mind about this but you have to choose the name which clearly define the purpose of the variable.this is actually a good habit to understand the program easily for some other developer. Consider the following example:

var x = "Max"
fmt.Println("My friend's name is", x)

In this case x is not a very good name for a variable. A better name would be:

var name = "Max"
fmt.Println("My friend's name is", name)

or even a best approach is:

var friendsName = "Max"
fmt.Println("My friend's name is", friendsName)

You can see in last example var name is self descriptive.

Scope

Like in other languages here to we can use variable having  global and local scope.Below is the example:

package main

import "fmt"

func main() {
var x string = "Hello World"
fmt.Println(x)
}

Another way of writing this program would be like this:

package main

import "fmt"

var x string = "Hello World"

func main() {
fmt.Println(x)
}

Notice that we moved the variable outside of the main function. This means that other functions can access this variable.So in first example we are defining a function which is only accessible to parent function.While in second example variable have global scope and can be called in any other functions.

Defining Multiple Variables

Golang also has another shorthand when you need to define multiple variables:

var (
a =5
b =10
c =15
)

Use the keyword var followed by parentheses with each variable on its own line.

Example

Here’s an example program which takes in a number entered by the user and doubles it:

package main

import "fmt"

func main() {
fmt.Print("Enter a number: ")
var input float64
fmt.Scanf("%f", &input)

output := input * 2

fmt.Println(output)
}

Udemy Generic 728x90

Spread the word. Share this post!