Golang: Understanding Variables


Now that we understand Golang’s project structure and how to write our own packages and commands, it’s time to get to the language basics and learn about variables. We will learn them in a very practical way – by writing code 🙂

The keyword var is the basic form to define variables. A var statement can be at package or function level. Also, Golang puts the variable type after the variable name unlike in languages like C/C++ where the variable type is written before the variable name

C/C++:
int a;

Golang:
var a int

 

There are multiple syntax that can be used to define variables in Go

Define single variable

var variable_name variable_type
var variable_name variable_type = value

Define multiple variables

var variable_name1, variable_name2 type
var variable_name1, variable_name2 type = value1, value2

While defining initial values, you can choose to omit the type and can say

var variable_name1, variable_name2 = value1, value2

Group form

Variables can also be declared in a group form like

var(
    variable_name1 = 10
    variable_name2 = “abc”
)

Short assignment

Now using var and type can be laborious and boring at times. Golang has a short assignment operator := too. This is how it is used:

variable_name1, variable_name2 := value1, value2

It is important to note that short assignments can also be used in function scope. Try using it in the global scope and you will notice errors.

Special variable _

When we want to ignore value of a return statement or discard a value in general, then the special variable name _ is used

_, x = “abc”, 123

Here value “abc” is ignored and x is assigned value of 123.

Unused variables

Also note if a variable is declared in the program but is never used then the Go compiler considers this an error and throws it

./addition.go:11:6: i declared and not used

Variable scopes

Scope is a region of the program where a variable can exist and can’t be accessed outside it. Golang has three different scopes:

  1. Local variables: defined in a function or a block
  2. Global variables: defined outside of all the functions
  3. Formal parameters: used in definition of function parameters (treated as local variables inside the function and take precedence over global variables)

Let’s look at them in a code snippet, we will learn more about the function scope when we talk about functions in detail

package main

import "fmt"

/* global variable declaration */
var a int = 20;

/* local variable declaration in main function */
func main() {
    var a int = 10
    var b int = 20
    var c int = 0

    fmt.Printf("Value of a in main() = %d\n", a);
    c = sum( a, b);
    fmt.Printf("Value of c in main() = %d\n", c);
}

/* function to add two integers */
func sum(a, b int) int {
    fmt.Printf("Value of a in sum() = %d\n", a);
    fmt.Printf("Value of b in sum() = %d\n", b);
    return a + b;
}

Output:

Value of a in main() = 10
Value of a in sum() = 10
Value of b in sum() = 20
Value of c in main() = 30

Constants

Constant values are defined at compile time and can’t be changed during run time. They can be of any data type; some of the examples of constants are shown below:

const abc = “123”;
const evelocity float32 = 11.1

 

Great, hope you got a fair idea about using variables in Golang. Let us look at the data types supported by Golang in the next blog 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s