Golang program to create a class inside the module


In this article we will write a Golang program to create a class inside the module. There are no classes only structs are used in this program which will contain the data fields.

A module is a collection of the packages having go.mod file at its root. This file determines the module’s directory.

Using Child Struct

In this Method, we will write a Golang program to create a class inside the module using child struct, a function createstruct will be created which will be called from another module to print child info.

Algorithm

  • Step 1 − Import the package mymodule and fmt in the program to execute the program

  • Step 2 − Create a child struct with Name of type string and Age of type int

  • Step 3 − Create a function createstruct and pass a print statement in that function

  • Step 4 − Then this module will be imported in the other module using import statement

  • Step 5 − Pass the name and age of the child in the Child constructor

  • Step 6 − Call the function from the other module and the statement in that module will be printed

  • Step 7 − The print statement is executed using Println function from the fmt package

Example

The following example demonstrates the Golang program to create a class inside the module using child struct.

package mymodule

import "fmt"
type Child struct {
   Name string
   Age  int
}

func (c *Child) createstruct() {
   fmt.Printf("Hello, my name is %s and I'm %d years old\n", c.Name, c.Age)
}
import "mymodule"  

func main() {
   c := mymodule.Child{Name: "Rahul", Age: 10}  
   c.createstruct()  
}

Output

Hello, my name is Rahul and I’m 10 years old

Using Rectangle Struct

In this illustration, we will write a Golang program to create a class inside the module using Rectangle struct. The function area will be called in which the product of width and the height will be returned.

Algorithm

  • Step 1 − Import the package mymodule and fmt in the program to execute the program

  • Step 2 − Create a Rectangle struct with width and height

  • Step 3 − Create a function Area and in that function return the product of width and height

  • Step 4 − Import this module in another module and in the main pass the width and the height in the Rectangle constructor

  • Step 5 − Call the function Area and print the area of the rectangle

  • Step 6 − The print statement is executed using Println function from the fmt package

Example

The following example is a Golang program to create a class inside the module using Rectangle struct

package mymodule

type Rectangle struct {
   Width  float64
   Height float64
}


func (rect Rectangle) Area() float64 {
   return rect.Width * rect.Height
}
import "mymodule"  

func main() {
   rect := mymodule.Rectangle{Width: 6, Height: 4}
   fmt.Println("The area of the rectangle is:", rect.Area())
}

Output

The area of rectangle is 24

Using Name Struct

In this method, we will write a Golang program to create a class inside the module using the name struct. The function will be created in module1 and the module1 will be imported where main is executed. The variable is used as a pointer to the struct.

Algorithm

  • Step 1 − The program imports the fmt and main package where fmt helps in the formatting of the input and Output and main helps in producing executable codes

  • Step 2 − Create a demo_struct with one field Name

  • Step 3 − Then, create a function Get_name where the pointer of the demo_struct is pointing to the s variable

  • Step 4 − In the function using the dot notation with the s return the Name

  • Step 5 − Import the module in the file where main is executed

  • Step 6 − In the main set the field value of the struct

  • Step 7 − Call the function using the dot notation with the s

  • Step 8 − Print the Output on the console using Println function from the fmt package

Example

The following example is Golang program to create a class inside the module using the name struct

package main

import "fmt"

type demo_struct struct {
   Name string
}

func (s *demo_struct) Get_name() string {
   return s.Name
}
package main

import "module1"

func main() {
   s := &module1.demo_struct{Name: "Ritika"}
   name := s.Get_name()
   fmt.Println(name) 
}

Output

Ritika

Conclusion

We compiled and executed the program of creating a class inside the module using three examples. In both the examples we created a struct inside a module and imported that module in another module but in the first case we used a struct with child, in the second example we used struct with the rectangle and in the third example we returned the name using Struct.

Updated on: 04-Apr-2023

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements