Skip to main content

Primitive data type of Golang


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

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 data

Pointer 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 address

Dereferencing 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 holds

on 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

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 )
        
    }
    

output :
    
    0 1 2
    0 1 2
    6 7 8
    6 4
    

as you can see iota evolve the value of constant through the constant block, and it will reset its value for every constant block. So every time iota is used it will increment its value by one and it will reset the value for new constant block.

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



Primitive data type of Golang

Comments

Popular posts from this blog

CRUD with Golang

  Overview this blog will be about a  basic crud app with golang .   this app will provide API to store, update, delete information . you need to know the basics of golang to understand this project. if you do not have the basics knowledge  about golang I would suggest you to read my previous blog.  I have uploaded my code in  GitHub. click the link to clone or download this project  I am going to talk about the topic below. Project setup Data handling controller Project setup  Initiate mod and set up project as per golang. if you don't know how to setup golang project i would suggest you to have a look at my blog  Creating a project with golang . this is a really short blog. you will have a quick over view of how to setup a project with golang.  so now that you have setup the project then the folder structure will be as below. Data Handling data store, delete and update will be handled in the user module. code is given below  package moduls import ( "errors&quo

Creating a project with Golang

Overview    You can run a go file with out making a project. But the standard way to use go is to create a project or you can call it a space where you will be putting your go code, on the other hand it is a work space, but in Go Lang we call it module. In Go Lang modules are the official way of organize go code. in this blog I am going to explain the way you create a model and first hello world with that project. Explanation To Module you need to run the command bellow go mod init <module name>  If you can see I have named my module github.com/TahimFaisal/golang. the name of the module is called module initializer. this is the standard way of initialization. And for the go get command it will it will know where to go and get the mod if it cannot find the mod locally.  Okay, soon after you ran the command you will get a file named go.mod in your working directory  it will be having module initializer and the version of go your using.     now create a main.go file and write