Golang Program to Determine the class of an object


What are Objects in Golang?

Objects are defined on the structs and store Key-value pairs of data together in go programming. There are numerous methods for producing an object. A struct is a user-defined type that groups together a collection of fields (also known as properties or data members) and methods. A struct can be defined using the type keyword, followed by the struct name and a struct literal that defines the fields of the struct.

Method 1: Using a user-defined function

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Then we have defined a structure of name Student that stores student’s credentials like name, roll number, class and admission number in the form of strings and integers.

  • Step 3 − Then we define the properties that our structure should have followed by the data type it should take.

  • Step 4 − 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.

  • Step 5 − Calling the main() function.

  • Step 6 − Creating a stud_1 object on the class Student declared above and storing the key value pairs of Student type in it.

  • Step 7 − Calling the printDetails() function

  • Step 8 − Printing the details on the screen using fmt.Println() function.

Example

In this example, we will write a go language program to create an object of a class. We will create a class and define an object from it. Further, we will store data to the object and print that on the screen.

package main
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")
   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() {
   var stud_1 = Student{name: "Nitin Sharma", rollNo: 21, class: 7, admissionNo: 2485}
   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

Method 2: Using structs and New Keywords

In this method, we are using structs in the 1st example and new kyewords in the second example to determine the class of the object in go programming.

Algorithm

  • Step 1 − First, we import the fmt package.

  • Step 2 − Next, we create a structure named Employee and define keys in it like name, age, salary, etc.

  • Step 3 − Then we call the main() function.

  • Step 4 − Create an object named newEmp from Employee class and pass the values to the keys as comma separated values.

  • Step 5 − Our newEmp object contains all the necessary data we can print on the screen using fmt.Println() function.

  • Step 6 − To access any of the property of the object we need to use “.” notation after specifying the name of the object followed by the property that we wish to access.

Example 1

Go Program to create a class of an object by passing comma separated values to structs

package main
import "fmt"

// defining a structure named Employee and adding some data to it
type Employee struct {
   Name string
   Age int
   Designation string
   Salary int
}
func main() {

   // creating the object named newEmp
   var newEmp = Employee{"Nitin", 27, "Developer", 40}
   fmt.Println("The age of employee is:", newEmp.Age)
   fmt.Println("The name of employee is:", newEmp.Name)
}

Output

The age of employee is: 27
The name of employee is: Nitin

Example 2

Using the new keyword is another well-liked method of creating an object.

package main
import "fmt"

// defining a structure named Employee and adding some data to it
type Employee struct {
   Name string
   Age int
   Designation string
   Salary int
}
func main() {

   // creating an empty object named newEmp.
   var newEmp = new(Employee)
   newEmp.Name = "Ram"
   newEmp.Age = 30
   fmt.Println("The age of employee is:", newEmp.Age)
   fmt.Println("The name of employee is:", newEmp.Name)
   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 program to determine the class of an object along with examples. Here we have shown three such methods via which we can create the objects from a struct or class in go and the ways by which we can access the data present in such an object.

Updated on: 16-Feb-2023

69 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements