Skip to main content

Variable declarations in Go language

Every programming language in this world provides options to declare and define variables and used by parts of the program. So Go does the same.

The wiki states that a variable is
a symbolic name associated with a value and whose associated value may be changed
But in more technical way, a variable is a memory location with name which might hold values and may change later.

Variables in memory

Go provides different options to declare and define the variables. Let's see how we can declare variable in Golang.

Variable declaration

A variable in go could be declared as follows,

package main

import "fmt"
var a = "has some value"

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

Here the variable a is declared and initialized with the value "has some value". The type of the variable is inferred from the value been assigned.

Supported Types in Golang

Go supports the following primitive types

Base Types Description
Numerics  uint, int (8,16,32,64 bits), float (32, 64 bits), 
 complex with 64, 128 bits, byte, rune, uintptr 
Numerics are integers and float type which
includes complex as well
String string String is an immutable; sequence of bytes
Boolean bool Boolean is either true or false

Group variable declaration

As we could declare multiple variables in a statement in other languages, Go also supports grouping the variable declarations.

var (
  a = 2
  b = 4
  c = 6
)

Note that we can't use comma to separate the variable but it has to be a newline.

The above code could be interpreted as shown below in three lines,

var a = 2
var b = 4
var c = 6

And, with grouping, we can declare multiple types. But in other programming languages like you can't declare multiple variables of different types


var (
  a = 2
  b = "some value"
  c = 1.5e2
)

Shorthand Variable declaration

Go has new form of variable declaration with the operator := which is called short-hand operator.

package main

import "fmt"

func main() {  
    str:= "value here" //short hand declaration

    fmt.Println("Prints some ", str)
}

The above shorthand declaration would be interpreted as string str = "value here".

Scope of the Variable

The variables declared in golang programs has block level scope. A block is a space enclosed within opening and closing curly brackets { ... }

package main

import "fmt"

func main() { 
    {
       str:= "value here" //short hand declaration

       fmt.Println("Prints some ", str)
    }
    
    fmt.Println("Prints some ", str) // 
}

When you execute the program above it would print something like,


>main.go 
undefined: str

Because the str is not available outside the curly brackets.

Global variables

Global variables could be declared outside the functions.

package main

import "fmt"

var some int

func main() {
   some = 10
   fmt.Println("Prints some ", some)
   some += some
   other()
}

func other() {
   fmt.Println("Prints some ", some)
}

When you execute the above code, it could print,

>main.go 
Prints some 10
Prints some 20

As you see, we could able to access the variable from both main and other functions.

Conclusion

In this blog post, we have discussed about declaring variables in golang and their multiple forms.

Thanks for reading 🙏 Comments are always welcome 🙌

Comments

Popular posts from this blog

Control Statements in Golang

The beauty of every programming language is defined by the flow of the program i.e by the Control Statements!. Yes, Control statements defines the flow of the program through the blocks of  statements defined. Go language provides control statements as like other languages but with different usage possibilities.

Types Of Control Statements There are three types of control statements available with Golang: Conditional, Unconditional and Loop.
Conditional Statements Conditional statements are statements which would execute the branch of statements conditionally. They are if, if-else , else-if and switch.
If statementif control statement controls the execution of the branch of statements based on the condition given. The syntax is,
if conditions { // statements to be executed } Let's look at the below example
if age == 5 { fmt.Println("Come back when you turn 6 to join 1st standard!") } In Golang, we can have initialization statement right before the conditional …

How to start with Go Language - A Google's Programming Language

It's been always fun to start programming with a brand new language. And, as a Programmer, it's absolutely worth to keep you updated with the new languages as you come across.

Every programming language in this world is been designed to solve a problem in mind. So learning a new language would help you to solve your problem efficiently if the language offers flexibility to do so.

I am going to share the knowledge on the new language; atleast for me 😉 (released on 10 November 2009) which i have explored on. The name of new language is Go. Yes it is Google's language; Golang.

Will see what Google says about the new Language,
Go is a statically typed, compiled language in the tradition of C, with memory safety, garbage collection, structural typing, and CSP-style concurrency. Yes, Go language is mainly designed to deal with morden days problems in the computing world. The Golang has the built-in feature of concurrency i.e deal with multiple tasks at same time which would le…