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

Golang Commands

Overview    In this blog I am going to explain about basic go command and in depth knowledge about go commands will come after this series of basic go programming end. fallowing topic will be explained in this blog : primary commands help topic Commands Go ahead open command prompt on type go and press Enter. you can see all the commands and help topic that go provide. Primary Commands   this all are primary commands that go provide. this all are self explanatory. we have "bug" command for bug report, "clean" to clean previous projects, "install" to install packages to project.mod command needs explanation. which we will discuss on the next blog. but the command I want to talk about is doc. if we run ''go doc <package name>'' command  example "go doc json.decoder.decode" it will show the out put below. and if you run "go doc json.decoder" it will show the out put below. and if you run "go doc json" ...

List of data types in Golang

  index Description 1 Type: int8 8-bit signed integer 2 Type:  int16 16-bit signed integer 3 Type:  int32 32-bit signed integer 4 Type:  int64 64-bit signed integer 5 Type:  uint8 8-bit unsigned integer 6 Type:  uint16 16-bit unsigned integer 7 Type:  uint32 32-bit unsigned integer 8 Type:  uint64 64-bit unsigned integer 9 Type:  int Both in and uint contain same size, either 32 or 64 bit. 10   Type:  uint Both in and uint contain same size, either 32 or 64 bit. 11 Type:  rune It is a synonym of int32 and also represent Unicode code points. 12 Type:  byte It is a synonym of int8 . 13 ...

Functions and Methods of golang

  Overview In this blog i am going to talk about the functions and methods in golang. which is a important topic in go. if you have not read the previous blog or do not have a basic knowledge about go the i would suggest you to have a look at my other blog.  Functions   Methods Functions you can declare a function as below. you don't need to declare return type unless your not returning something from your function. you get the return value in a way that is explained in the example code . func <function name > ( <parameter> <parameter type> ) ( <return type> ){} Example Code : package main import ("fmt") func main() { test1() // example 1 sum := test2() // example 2 fmt.Println("sum =",sum) sum2,error:= test3(30,40) // example 3 fmt.Println("sum 2 =",sum2,error) sum3,_:= test3(80,60) // example 4 fmt.Println("sum 2 =",sum3) su...