Overview
In this blog I am going to talk about the date types and variables in go. and the topics bellow
- Declaring and initializing variables
- explicit initialization of variables
- implicit initialization of variables
- complex data type
- list of data type
- Pointer
- Constant
- Iota and constant expression
Declaring and initializing variables
there are 2 ways you can declarer and initialize a variable they are
- explicit initialization variables
- implicit initialization variables
Explicit initialization of variables
in this way you have to tell the compiler every thing you need to declare a variable.
var i int
i = 20
var j int = 3
fmt.Println(i,j)
var f float32 = 32.233
fmt.Println(f)
in this way we are explicitly telling what will be the input type for that variable.
Implicit initialization of variables
in this way of declaring a variable. we don't have to tell the compiler what type of data it will take on that variable. compiler will set the variable type automatically by the type of data it will get.
name := "faisal"
fmt.Println(name)
Complex Datatype
one more important thing on Golang is it provides complex data type built in, to calculate complex number.
com := complex(3,4)
fmt.Println(com)
r, im := real(com) , imag(com)
fmt.Println(r,im)
check out the list of data type in Go Lang on this link
Pointer
Pointer is really important data type in golang. pointer is a data type which will hold the memory location. you can declare a pointer as below
var ptr *string = new(string)
code above says that the variable 'prt' is a pointer type or that variable will hold a memory which will hold string dataPointer operator
'*' is the pointer operator which is used to declarer pointer type
Address Of operator
'&' is a address of operator. which will return the address of any variable. we can use this operator to find memory address of any variable. name := "helloname"
var ptr *string = new(string)
ptr = &name
fmt.Println(ptr)
the code above says that the pointer 'ptr' holds the memory address of 'name' variable which is a string type. out put will be a memory addressDereferencing operator
'*' is a dereferencing operator. dereferencing is used to get the data from a memory location. if you got a memory location and you want to know what is on that memory location. then you just need to dereference that location. And it will return the value.
code :
name := "helloname"
var ptr *string = new(string)
ptr = &name
fmt.Println(ptr,*ptr)
out put :
0xc000010240 helloname
first one is a memory location where 'name' variable is stored when we dereference it then we get the value that memory address holdson example 1:
name := "helloname" var ptr *string = new(string) *ptr = name fmt.Println(ptr,*ptr)
out put of example 1 :
0xc000010240 helloname
in the code we dereference the 'name' variable in to 'ptr' pointer variable.on example 2:
name := "helloname"
var ptr *string = new(string)
ptr = &name
fmt.Println(ptr,*ptr,name)
*ptr = "hello world !!"
fmt.Println(*ptr,name)
Out put of example 2 :
0xc00009e220 helloname helloname
hello world !! hello world !!
Note : as you can see i have changed the '*ptr' but change is showing on 'name' variable. because by doing '*prt' I am telling computer to change the value on that memory address. check out the more details about pointer in Golang on this link
Constant
constant work just like the variable but the difference is variable can change there value over time, but constant cant.
package main
import ("fmt")
func main(){
const c int = 3
const pie float32 = 3.14159
fmt.Println( float32(c) + pie )
}
you can you explicate or implicate declaration to declare constant. all the line is parity much self explanator but the "float(c)". it is just converting the int type to float type. you can use constant block to declare constant.
package main
import ("fmt")
const (
first = "hello"
second = "world"
third = "!!!"
)
func main() {
fmt.Println( first,second,third )
}
Note : we need to initialize the constant at the time we are declaring them. a constant cant change its value so you cant initialize it after it is been declared. and other thing is, the value of the constant has to be determined at compile time. so you cant initialize a constant to a return value of a function because functions aren't actually evaluated until runtime with go. so since you cant evaluate them at compile time you cant actually use them to initialize a constant.
Compile time is when the program is compiled source code into a machine code; runtime is when it executes
Compile time is when the program is compiled source code into a machine code; runtime is when it executes
Iota and Constant expression
Iota allows us to evolve constant through out a constant block and constant expression gives us a wide verity of things we can do as we generate the value of that constant. it will be clearer in an example.
Code:
package main
import ("fmt")
const (
first = iota
second = iota
third = iota
)
const (
forth = iota
fifth = iota
sixth = iota
)
const (
testOne = iota + 6
testTwo
testThree
)
const (
testFive = iota + 6
testSix = 2 << iota
)
func main() {
fmt.Println( first,second,third )
fmt.Println( forth,fifth,sixth )
fmt.Println( testOne,testTwo,testThree )
fmt.Println( testFive,testSix )
}
0 1 2
0 1 2
6 7 8
6 4
on the other hand, you can see i have used constant expression on testFive and testSix constant. and used bit manipulation on testSix constant. To know details about bit manipulation click on the link
Previous Blog | Next Blog |
---|---|
Golang Commands | Collections of Golang |
Comments
Post a Comment