Skip to main content

Pointer in Golang

 

Over view

In this blog I am going to talk about pointer and how it works. will talk about 
to understand pointer one need to know how computer store its value in memory. By memory i will be refereeing to the RAM.

basically computer stores its variable in memory in binary format. and when some one declares a variable it will allocate some bite of memory for that variable. And when variable is initialized it will go to its look up table and search for the first memory address and store data.

for int and float it will allocate 4 and char it will allocate 1 byte of memory  

suppose I have declared 'a' as variable which is type int. computer will allocate 4 byte of memory for that and if i declared 'c' as variable with a type char. it will allocate a 1 byte of memory for this.
and it will put the first memory address to its lookup table to search for the variable later. then if I initialize the variable then it will store the data in that memory location as a binary format.    

Pointer

pointer is a variable that stores address of another variable. Basically pointer will point to a memory where a variable is stored. Suppose 'a' has a memory address of 203, and 'ptr' is a pointer type variable 
so if we initialize 'ptr' to 203 then 'ptr' will point to a memory location which is already holding the value of  variable 'a'

pointer

    
in the picture, 'a' holds 5 and 'b' holds 6.'ptr' holds 203 which is memory address of 'a' that means 'ptr' points to 'a' we can change the 'ptr' by adding 1. on that case it will point to 'b' which holds '6'. 
so we can say pointer is a variable which will hold only memory address.

Dereferencing

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.
	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

code explain: 'ptr' is a pointer type variable which will hold a memory location of which will hold a value of type string. 'name' is a string type variable. by doing '&name' we are getting the memory address of the 'name' variable. so by printing 'ptr' we get the memory location on 'name' and by doing '*ptr' we are getting the value of  'name' variable. This is what we call dereferencing. 

on other example: 
        name := "helloname"
	
        var ptr *string = new(string)
        *ptr = name

        fmt.Println(ptr,*ptr)
out put :
 0xc000010240 helloname
in the code we dereference the 'name' variable in to 'ptr' pointer variable.

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

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   explicit initialization of variables   implicit initialization of variables complex data type list of data type Pointer pointer operator dereferencing operator address of operator 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

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