Monday, November 12, 2018

Learn Golang tutorials - Variables starter guide with examples

In this blog post, It covers the Golang Tutorials variables  guide with examples
Learn Golang tutorials - Variables starter guide with examples

Golang Tutorials variables

Variables usage in go language is same as like another programming language like java. Variables are the name assigned for a space in memory that saves the values.  Variables of different types. Based on each type, memory size will be accommodated

Variables name can be named using letters, digits and underscore characters. Go lang is case sensitive so that upper and lower names are different. Variables in go language are declared using var keyword. before using any variables, you need to first declare it.

Variable declaration

Variables can be declared using var keyword, Once variable is declared, the compiler allocates memory based on the type of the variable. The variable declaration contains three parts 1. var keyword 2. variable name 3. type of variable
var variablename(s) variableType
can be a single variable or multiple variable types are predefined inbuilt types or custom types. Inbuilt types can be byte, int and float32 and string. Following are a various way of variable declaration examples
initialized variables
It can be declared and initialized with values. This can be done in many ways
The following example explains converting different types
package main
import (
	"fmt"
)
func main() {
	m := 42
	n := float64(2.0)
	var o float64 = float64(25.2)
	fmt.Println(m)
	fmt.Println(n)
	fmt.Println(o)

}
Output is
42
2
25.2
Multiple variable declarations 
Here multiple variables can be declared in a single line of the same type
var  a,b,c int;
var (
  m = 1
  n = 20
  o = 3
)
Short variable declarations 
variables can be declared and assigned with initial values. Multiple variables can be declared in a single line and readability is improved and code lines reduced
package main
import "fmt"
func main() {
	var m, n int = 10, 20
	fmt.Println(m, n) // 10 20
}

Global Scope 

variables declared outside function blocks and at the package level. This variable can be accessed across all functions declared in the program. These variables can exist as long as the program is running
package main

import "fmt"

//Global Variables Declaration
var globalVariable="Hello";
func main() {
	fmt.Println(globalVariable);

}
and output of the above code is
Hello

Local Scope 

variables declared inside a function or block of a code is called local variable. The variables cannot be accessed outside the function
package main
import "fmt"
func main() {
	//Local Variables Declaration
    var localVariable="this is test variable";
	fmt.Println(localVariable);

}
and output of the above code is
this is test variable 

Variables default values 

variables are assigned with a default value when variables are assigned with initial values zero for number types like Int, Float etc.. false for Boolean types empty string - "" for Strings


package main

import "fmt"

func main() {
	var intVariable int32
	var floatVariable float32
	var booleanVariable bool
	var strVariable string
	fmt.Printf("%v %v %v %q\n", intVariable, floatVariable, booleanVariable, strVariable) // output 0 0 false ""

}

type inference 

type inference is called When a variable is declared without the type of the variable. variable type can derive the type from the right-hand side of the value. inference can be applied to variables and constants Below example descript using variable expression using = and := operators
package main
import "fmt"
func main() {
	var k int
	l:=k
	var m = 42 
	var n = false 
	p:=3.2 // constant 
	fmt.Printf("variable k datatype is %T\n", k)
	fmt.Printf("variable l datatype is %T\n", l)
	fmt.Printf("variable m datatype is %T\n", m)
	fmt.Printf("variable n datatype is %T\n", n)
	fmt.Printf("variable p datatype is %T\n", p)

}
the output of the above program code is
variable k datatype is int
variable l datatype is int
variable m datatype is int
variable n datatype is bool
variable p datatype is float64

Constants declaration 

Constants are like variables which can not be changed its value once the value is assigned. whereas a variable can be changed. Constants can be declared using := syntax. const can be applied to all types of variables like boolean, strings and numeric types
package main
import "fmt"
func main() {
	const str = "kiran"
	fmt.Println("Hi", str) // Hi kiran
	const booleanValue = true
	fmt.Println(booleanValue) // true
}

How to find a memory or address location of a variable ie variable pointer reference memory

memory for variables in golang can be referenced using  & variable name - ampersand variable. 
  • & the variable is a pointer of a variable which prints memory location of the variable 
  • *& the variable is a value of a variable at memory location
This example prints the pointer of a variable
package main
import "fmt"
func main() {
   var m int
    fmt.Println("memory location for m :", &m, "variable value is ", *&m)
    n:= 15
    fmt.Println("memory location for n :", &n, "variable value is ", *&n)
}
Output of the above code is
memory location for m : 0x416020 variable value is  0
memory location for n : 0x416024 variable value is  15

Related article


EmoticonEmoticon