
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 Class and Object in Go
In this article we are going to learn how to create class and object.
Structs ? Go language does not have classes. To create an object in the go programming language we can specify structs and store key-value pairs in it. A struct is a user-defined data type that is used to store data together. The values so stored may have the same or different data type.
Syntax
The syntax to define a structure is as follows ?
type name_of_struct struct { name_1 type_1 name_2 type_2 name_3 type_3 name_4 type_4 ? }
A new structure starts with a type keyword to specify that we are defining a new type then followed by the name of the structure and the struct keyword to specify that we are defining a new structure. A structure can store different values together of the same or different data type.
Object ? An object is used to encapsulate data together in the form of key-value pairs. There are different ways to create an object. We shall discuss some of them in this article.
Example 1
Golang program to create a class code ?
package main // fmt package allows us to print anything on the screen import "fmt" // defining a structure with name myStrunct type Student struct { // defining values of struct name string rollNo int admissionNo int class int } // function to student data type func (s Student) printDetails() { fmt.Println("Student Details...\n") // printing the structure details fmt.Println("Student name is:", s.name) fmt.Println("Student Admission number is:", s.admissionNo) fmt.Println("Student Class is:", s.class) fmt.Println("Student Roll number is:", s.rollNo) } func main() { // defining a variable name stud_1 of type Student var stud_1 = Student{name: "Nitin Sharma", rollNo: 21, class: 7, admissionNo: 2485} // calling the printDetails() function to print the structure details on the screen. stud_1.printDetails() }
Output
Student Details... Student name is: Nitin Sharma Student Admission number is: 2485 Student Class is: 7 Student Roll number is: 21
Description to The Above Code
First, we need to import the fmt package. This package allows us to print anything on the screen.
Then we have defined a structure of the name Student that stores the student's credentials like name, roll number, class and admission number in the form of strings and integers.
Then we define the properties that our structure should have followed by the data type it should take.
Next we have defined a function that takes a variable of type student as a parameter and prints the student's details on the screen using fmt.Println() function.
Calling the main() function.
Creating a stud_1 variable and store the key-value pairs of Student type in it.
Calling the printDetails() function
Printing the details on the screen using fmt.Println() function.
EXAMPLE 2: GO LANG PROGRAM TO CREATE AN OBJECT
Once we get our structure next task is to create objects out of it. There are different methods to create objects from the structs.
Method-1: Passing Comma-Separated Values To Structs
Once we get our structure next task is to create objects out of it. There are different methods to create objects from the structs.
Example
The simplest way to create an object is to pass the values in the order in which we have defined them in the structure directly.
package main import "fmt" // fmt package allows us to print anything on the screen // defining a structure named Employee and adding some data to it type Employee struct { Name string Age int Designation string Salary int } // calling the main() function func main() { // creating an object named newEmp by passing various comma //separated values to it. var newEmp = Employee{"Nitin", 27, "Developer", 40} // printing the age of the employee from the newEmp object fmt.Println("The age of the employee is:", newEmp.Age) // printing the name of the employee from the newEmp object fmt.Println("The name of the employee is:", newEmp.Name) }
Output
The age the of employee is: 27 The name the of employee is: Nitin
Description
First, we import the fmt package that allows us to print anything on the screen.
Next, we create a structure named Employee and define keys in it like name, age, salary, etc.
Then we call the main() function.
Create an object named newEmp from the Employee class and pass the values to the keys comma-separated values.
Now our newEmp object contains all the necessary data that we can print on the screen using fmt.Println() function.
To access any of the properties of the object we need to use "." notation after specifying the name of the object followed by the property that we wish to access.
NOTE ? If we do not specify the values of any of the properties defined in the structure while creating the object then some default values are chosen for such properties. Here are some default values.
String values get "" an empty string by default.
Integer values by default get 0 as the input value.
The Booleans by default store false.
Method 2: Create an Object With a New Keyword
Example
Another popular approach to creating an object is by using a new keyword. Let us see an example for more clarity.
package main import "fmt" // fmt package allows us to print anything on the screen // defining a structure named Employee and adding some data to it type Employee struct { Name string Age int Designation string Salary int } // calling the main() function func main() { // creating an empty object named newEmp. var newEmp = new(Employee) // assigning values to newEmp object newEmp.Name = "Ram" newEmp.Age = 30 // printing the age of the employee from the newEmp object fmt.Println("The age of employee is:", newEmp.Age) // printing the name of the employee from the newEmp object fmt.Println("The name of employee is:", newEmp.Name) // printing the default values fmt.Println("The default values for salary is:", newEmp.Designation, newEmp.Salary) }
Output
The age of employee is: 30 The name of employee is: Ram The default values for salary is: 0
Conclusion
We have successfully compiled and executed a go language code to create a class and a object along with examples.