
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
Create a Simple Class in GoLang
In this tutorial, we will be discussing the approach to creating a simple class in Golang programming.
However, before writing the code for this, let's briefly discuss the concept of class.
What is Class?
A class is a template or blueprint for creating objects. A class binds together all the elements of a program and it is an essential part of object?oriented programming.
Nevertheless, Go language does not support the ?class' keyword unlike other languages like Java, C++, etc. which are object?oriented languages.
However, this does not limit Go to using the functionality of a class. Go keeps up with the concept of classes without using the keyword ?class'.
Struct keyword in Go Language
Struct keyword is a powerful tool in Go that is similar to a class. This means that we can create a class in Go using struct without actually using the ?class' keyword.
By defining a struct we can define a class and by using struct type, we can also implement its methods. The behavior of a struct keyword is very similar to that of a class.
A struct, also known as a structure is a collection of different fields into one single field. They are used for grouping data as one.
Structs are user?defined and mutable.
Example
Assume that you want to store the details of an Employee of an organization. The details will include fields like Employee name, Employee ID, address, gender, age, etc. of different data types.
In order to store multiple values of different data types, a struct can be used where Employee can be the struct name and all the employee's details can be its fields.
type Employee struct { employeeName string employeeID int employeeEmail string employeeSalary float }
Demonstration of a Simple Class
Creating Employee Struct to resemble a Class
Algorithm
Step 1 ? Create a struct with the name ?Employee' and declare the fields like employee name, employee ID, etc. along with their data type.
Step 2 ? Create a function PrintDetailsOfEmployee() that will access the Employee struct's values and print the employees' details.
Step 3 ? In the main method, initialize the values to the employees. This will be done in form of key and value pairs.
Step 4 ? Print the details of the employees by calling the PrintDetailsOfEmployee() function.
Example
package main // fmt package allows us to print formatted strings import "fmt" // defining struct type Employee struct { employeeName string employeeID int employeeGender string employeeEmail string employeeSalary float32 } // this method will access the values of struct Employee func (emp Employee) PrintDetailsOfEmployee() { fmt.Println("Name : ", emp.employeeName) fmt.Println("ID: ", emp.employeeID) fmt.Println("Gender: ", emp.employeeGender) fmt.Println("Email Address : ", emp.employeeEmail) fmt.Println("Salary : ", emp.employeeSalary) } func main() { fmt.Println("Employee Details \n") // storing employee details var emp1 = Employee{employeeName: "Rahul Singh", employeeID: 50062, employeeGender: "Male", employeeEmail: "[email protected]", employeeSalary: 65000} var emp2 = Employee{employeeName: "Shreya Goel", employeeID: 50132, employeeGender: "Female", employeeEmail: "[email protected]", employeeSalary: 57000} // printing employee details emp1.PrintDetailsOfEmployee() emp2.PrintDetailsOfEmployee() }
Output
Employee Details Name : Rahul Singh ID: 50062 Gender: Male Email Address : [email protected] Salary : 65000 Name : Shreya Goel ID: 50132 Gender: Female Email Address : [email protected] Salary : 57000
Description of code
var emp1 = Employee{employeeName: "Rahul Singh", employeeID: 50062, employeeGender: "Male", employeeEmail: "[email protected]", employeeSalary: 65000} ? Here, we are declaring the values of Employee in the form of key and values, where the key is the employee field and value is the employee's information.
Creating a Rectangle struct to resemble a Class
Step 1 ? Create a struct with the name ?Rectangle' and declare the fields like length and breadth along with their data type.
Step 2 ? Create a function CalculateArea() to print the details of the rectangle and calculate its area using the formula length * breadth and print it.
Step 3 ? In the main method, initialise the values to the rectangle struct and store its values in the form of key and value pairs.
Step 4 ? Print the rectangle's length, breadth, and area by calling the CalculateArea() function.
Example
package main // fmt package allows us to print formatted strings import "fmt" // defining struct type Rectangle struct { length int breadth int } // this method will access the values of struct Rectangle func (rect Rectangle) CalculateArea() { fmt.Println("Length : ", rect.length) fmt.Println("Breadth : ", rect.breadth) fmt.Println("Therefore, Area : ", rect.length*rect.breadth) } func main() { fmt.Println("Rectangle Details \n") var rect1 = Rectangle{length: 5, breadth: 2} var rect2 = Rectangle{length: 25, breadth: 10} rect1.CalculateArea() rect2.CalculateArea() }
Output
Rectangle Details Length : 5 Breadth : 2 Therefore, Area : 10 Length : 25 Breadth : 10 Therefore, Area : 250
Description of code
var rect1 = Rectangle{length: 5, breadth: 2} ? Here, we are declaring the values of Rectangle in the form of key and values, where the key is the rectangle field and value is the rectangle's information.
Conclusion
This is all about creating a class in Golang programming where we elaborated with the help of two examples on how the absence of the ?class' keyword in Go does not limit us from using the functionality of a class. You can explore more about Golang programming using thesetutorials.