Go Tutorial - Section 5

Welcome to the fifth section of this Go Tutorial Series. I hope you have completed your second assignment and got your feedback. If you haven't heard from me with feedback, please be assured that I will get back to you as soon as possible. In this section, we are going to cover two very important topics.

  1. Pointers

  2. Structures


Pointers


Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. Like any variable or constant, you must declare a pointer before you can use it to store any variable address. The general form of a pointer variable declaration is

var var_name *var-type

Here, type is the pointer's base type; it must be a valid data type and var-name is the name of the pointer variable. The asterisk * you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement, the asterisk is being used to designate a variable as a pointer.

package main

import "fmt"

func main() {
   var a int = 20   /* actual variable declaration */
   var ip *int      /* pointer variable declaration */

   ip = &a  /* store address of a in pointer variable*/

   fmt.Printf("Address of a variable: %x\n", &a  )

   /* address stored in pointer variable */
   fmt.Printf("Address stored in ip variable: %x\n", ip )

   /* access the value using the pointer */
   fmt.Printf("Value of *ip variable: %d\n", *ip )
}

Output:

Address of var variable: 10328000
Address stored in ip variable: 10328000
Value of *ip variable: 20

Nil Pointers


Go compiler assign a Nil value to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned nil is called a nil pointer.

package main

import "fmt"

func main() {
   var  ptr *int

   fmt.Printf("The value of ptr is : %x\n", ptr  )
}

Output:

The value of ptr is 0

To check for a nil pointer you can use an if statement as follows

if(ptr != nil)     /* succeeds if p is not nil */
if(ptr == nil)    /* succeeds if p is null */

Array of Pointers


There may be a situation when we want to maintain an array, which can store pointers to an int or string or any other data type available. The following statement declares an array of pointers to an integer.

var ptr [MAX]*int;

This declares ptr as an array of MAX integer pointers. Thus, each element in ptr, now holds a pointer to an int value. The following example makes use of three integers.

package main

import "fmt"
 
const MAX int = 3
 
func main() {
   a := []int{10,100,200}
   var i int
   var ptr [MAX]*int;

   for  i = 0; i < MAX; i++ {
      ptr[i] = &a[i] /* assign the address of integer. */
   }
   for  i = 0; i < MAX; i++ {
      fmt.Printf("Value of a[%d] = %d\n", i,*ptr[i] )
   }
}

Output:

Value of a[0] = 10
Value of a[1] = 100
Value of a[2] = 200

Pointer to Pointer


A pointer to a pointer is a form of a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below.

A variable that is a pointer to a pointer must be declared as such. This is done by placing an additional asterisk in front of its name

var ptr **int;

Example:

package main

import "fmt"

func main() {
   var a int
   var ptr *int
   var pptr **int

   a = 3000

   /* take the address of var */
   ptr = &a

   /* take the address of ptr using address of operator & */
   pptr = &ptr

   /* take the value using pptr */
   fmt.Printf("Value of a = %d\n", a )
   fmt.Printf("Value available at *ptr = %d\n", *ptr )
   fmt.Printf("Value available at **pptr = %d\n", **pptr)
}

Output

Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000

Structures


A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct. This concept is generally compared with the classes in object-oriented programming. It can be termed as a lightweight class that does not support inheritance but supports composition.

Structures are used to represent a record. Suppose you want to keep track of the books in a library. You might want to track the following attributes of each book:

  • Title

  • Author

  • Subject

  • Book ID

In such a scenario, structures are highly useful.

type Address struct {
      title string 
      author string
      subject string
      book_id int
}

n the above, the type keyword introduces a new type. It is followed by the name of the type (Address) and the keyword struct to illustrate that we’re defining a struct. The struct contains a list of various fields inside the curly braces. Each field has a name and a type.


To access any member of a structure, we use the member access operator (.).The member access operator is coded as a period between the structure variable name and the structure member that we wish to access.

package main

import "fmt"

type Books struct {
   title string
   author string
   subject string
   book_id int
}
func main() {
   var Book1 Books    /* Declare Book1 of type Book */
   var Book2 Books    /* Declare Book2 of type Book */
 
   /* book 1 specification */
   Book1.title = "Go Programming"
   Book1.author = "Mahesh Kumar"
   Book1.subject = "Go Programming Tutorial"
   Book1.book_id = 6495407

   /* book 2 specification */
   Book2.title = "Telecom Billing"
   Book2.author = "Zara Ali"
   Book2.subject = "Telecom Billing Tutorial"
   Book2.book_id = 6495700
 
   /* print Book1 info */
   fmt.Printf( "Book 1 title : %s\n", Book1.title)
   fmt.Printf( "Book 1 author : %s\n", Book1.author)
   fmt.Printf( "Book 1 subject : %s\n", Book1.subject)
   fmt.Printf( "Book 1 book_id : %d\n", Book1.book_id)

   /* print Book2 info */
   fmt.Printf( "Book 2 title : %s\n", Book2.title)
   fmt.Printf( "Book 2 author : %s\n", Book2.author)
   fmt.Printf( "Book 2 subject : %s\n", Book2.subject)
   fmt.Printf( "Book 2 book_id : %d\n", Book2.book_id)
}

Output

Book 1 title      : Go Programming
Book 1 author     : Mahesh Kumar
Book 1 subject    : Go Programming Tutorial
Book 1 book_id    : 6495407
Book 2 title      : Telecom Billing
Book 2 author     : Zara Ali
Book 2 subject    : Telecom Billing Tutorial
Book 2 book_id    : 6495700


This brings me to the end of this section. We will follow this with an assignment that will revise your previously studied topic and give a better explanation of the topics covered here.

 

+919853340643

©2020 by Aman Sinha.