
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use of 'this' Keyword in Golang
In Go programming language, there is no concept of classes so struct are used to demonstrate the use of this keyword in class. The "this" keyword refers to the current method or object that is currently being executed in the program.
In this article we will use two examples to see how the program works. In the first example, we will use Child struct by calling the method on this struct and printing its name and age whereas in the second example, we will use a Rectangle struct to print its area by calling the method on this struct. Let's have a look at the practical implementation to understand the program.
Algorithm
Import the fmt package in the program which will help in the formatting of the input and the output
Create a struct with the required fields
To demonstrate the use of this keyword, create a method with no argument and call the method on the struct
In the main function set the values in the instance and call the method
As the method is called the fields of the current instance will be printed on the console
The print statement is executed using fmt.Println function from the fmt package
Example 1
In this example, we will create a Child struct with two fields Name and age. A greet function is created on the Child struct with no arguments and when the method will be called the statement inside it will be printed as output. Here, c.Name and c.Age will refer to the current instance. Let's see how the code is executed.
package main import "fmt" type Child struct { Name string Age int } func (c Child) greet() { fmt.Printf("Hello, my name is %s and I'm %d years old.\n", c.Name, c.Age) } func main() { c := Child{"Veronica", 16} //set the values of the instance c.greet() //call the method }
Output
Hello, my name is veronica and I'm 16 years old.
Example 2
In this illustration, we create the struct similarly like we did in the last example. Here, two fields width and height of the rectangle will be multiplied to get the area of the current instance. In the main, the area of the rectangle will be printed when the method Area() will be called. Let's see how the code executes.
package main import "fmt" type Rectangle struct { Width float64 Height float64 } func (rect Rectangle) Area() float64 { return rect.Width * rect.Height } func main() { rect := Rectangle{6.0, 10.0}//set the values in the instance fmt.Printf("Area of rectangle with width %.2f and height %.2f is %.2f\n", rect.Width, rect.Height, rect.Area()) //print the area of the rectangle }
Output
Area of rectangle with width 6.00 and height 10.00 is 60.00
Conclusion
We executed and compiled the program showing the use of this keyword in class using two examples. In the first example, the child struct was used and in the second example the rectangle struct was used to demonstrate the use of this keyword. Here, it is demonstrated via calling the method on the struct and printing the field values.