Golang Program to Print object of a class


What is the Object of a Class?

Object stores Key-value pairs of data together. There are numerous methods for producing an object. Some of them will be covered in this post.

In this article, we are going to use two different methods to print objects of a class in the go programming language.

Method 1: Using a Structure

In this method, we will first create some structures and in the main() produce objects out of them. further we will store properties to that objects and print them on the screen. This can be obtained by following Steps.

Algorithm

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

  • Step 2 − To create a class initialize a new struct type and define properties in it that we wish to define in the class. In this particular example we have create two structures one of name as student and another with name Animals.

  • Step 3 − Student class has three properties defined in it as Id, Name and Fees whereas the Animals class has properties like species, color and price.

  • Step 4 − Call the main() function. This is the starting point for our program.

  • Step 5 − Creating the Student1, Student2 and Student3 objects from the Student class and providing the values to keys initialized. Now print the Objects on the screen using fmt.Println() function.

  • Step 6 − Similarly creating two objects from the animals class as animal1 and animal2 and providing the values to its keys as well. Print the respective objects on the screen using fmt.Println() function.

Example

In this program, we will write a go language program to print the objects of a class by using a structure

package main
import "fmt"

// Creating a structure named Student
type Student struct {
   Id   int
   Name string
   Fees int
}

// Creating a class named Animals 
type Animals struct {
   species string
   color   string
   price   int
}
func main() {
   // Creating different objects from the Student class
   student1 := Student{Id: 101, Name: "Kapil", Fees: 10000}
   student2 := Student{Id: 102, Name: "Amit", Fees: 12000}
   student3 := Student{Id: 103, Name: "Arun", Fees: 15000}

   // Printing the objects created above on the screen
   fmt.Println("Student's Information:")
   fmt.Println("\nStudent1:", student1)
   fmt.Println("\nStudent2:", student2)
   fmt.Println("\nStudent3:", student3)
   fmt.Println()

   animal1 := Animals{species: "Dog", color: "white", price: 10000}
   animal2 := Animals{species: "Cat", color: "Red", price: 1000}

   fmt.Println("Animal's Information:")
   fmt.Println(animal1)
   fmt.Println(animal2)
}

Output

Student's Information:

Student1: {101 Kapil 10000}

Student2: {102 Amit 12000}

Student3: {103 Arun 15000}

Animal's Information:
{Dog white 10000}
{Cat Red 1000}

Method 2: Using New Keyword

In this method, we are creating and defining a structure then with the help of new keyword we are creating the object to that structure.

Algorithm

  • Step 1 − 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 using new keyword and pass the values to the keys as comma separated values.

  • Step 5 − Now our newEmp object contains all the necessary data that 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.

NOTE − When creating an object, if we don't supply values for any of the structure's declared properties, some default values are selected. Here are a few typical values.

  • By default, string values for "" are empty strings.

  • The input value for integer numbers is always 0 by default.

  • The Booleans always store false by default.

Example

In this example we have created a go language program to print the objects of the class using new keyword.

package main
import "fmt"

// Defining a structure named Employee
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
   newEmp.Designation = "Senior Developer"
   newEmp.Salary = 50000

   fmt.Println("Printing the employee object")
   fmt.Println(newEmp)
   fmt.Println()
   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.Salary)
}

Output

Printing the employee object
&{Ram 30 Senior Developer 50000}

The age of employee is: 30
The name of employee is: Ram
The default values for salary is: 50000

Conclusion

We have successfully compiled and executed a go language program to print the objects of the classes on the screen along with the examples. Here we have created two examples. In the first one we have used the equality operator to create the object and store data to it while in the second one we have used new keyword to create the object.

Updated on: 16-Feb-2023

580 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements