Golang Program to Create an Interface Named Worker That Defines a Work Method


The work method in the go language is a user-defined method for custom data types to perform various operations. You can choose any name but functionality will depend on the specific requirement. The Worker interface can be executed by different sorts to supply distinctive usage of the Work behavior. Here we are going to use three different methods: direct interface implementation, struct embedding as well as interface assertion along with examples to elaborate the concept. In this article, we are going to investigate how to form an interface named Laborer in Go that indicates a Work strategy.

Syntax

type Animal interface {Work() string}

The Syntax type Animal interface { Speak() string } defines an interface named Animal in Go, specifying that any type implementing this interface must have a method named Speak that takes no arguments and returns a string.

type OuterStruct struct

OuterStruct embeds EmbeddedStruct by including it as a field within OuterStruct.

EmbeddedStruct

The embedded struct's fields and methods are automatically accessible within OuterStruct.

func (o OuterStruct)

Methods specific to OuterStruct can be defined, and methods of EmbeddedStruct can be accessed using o.EmbeddedStruct.Method().

value, ok := variable.(InterfaceType)

The Syntax value, ok := variable.(InterfaceType) is used in Go for interface assertion. It attempts to assert that the value stored in variable implements the InterfaceType, and if successful, assigns the asserted value to value and sets ok to true; otherwise, ok is set to false.

Algorithm

  • Step 1 − Declare an interface named Worker with a single method signature: Work().

  • Step 2 − Implement different types, such as Manager, Engineer, etc., that satisfy the Worker interface by providing their own Work method implementations.

  • Step 3 − In the main function or any other relevant context, create instances of various worker types and assign them to variables of type Worker.

  • Step 4 − Invoke the Work method on each worker variable. Since all worker types implement the Worker interface, the appropriate Work method implementation will be called based on the underlying type.

  • Step 5 − Repeat step 4 for any additional worker instances or types you want to include.

  • Step 6 − Optionally, you can perform type assertions or use type switches to check the specific type of each worker instance.

  • Step 7 − Run the program and observe the output, which will demonstrate different work behaviors based on the underlying worker types.

Example 1

In this code example, we define an interface named Worker with a single method Work().we create two types, Manager and Engineer, which implement the Worker interface by providing their own Work() method implementations and prints the corresponding output.

package main

import "fmt"

type Worker interface {
   Work()
}

type Manager struct{}

func (m Manager) Work() {
   fmt.Println("Manager is working...")
}

type Engineer struct{}

func (e Engineer) Work() {
   fmt.Println("Engineer is working...")
}

func main() {
   manager := Manager{}
   engineer := Engineer{}

   manager.Work()
   engineer.Work()
}

Output

Manager is working...
Engineer is working...

Example 2

This code demonstrates Struct Embedding in Go. It defines an interface named Worker with a Work() method. The code includes a struct called EmbeddedStruct with its own Work() method implementation. Another struct named OuterStruct is created, which embeds EmbeddedStruct. The OuterStruct struct inherits the Work() method from EmbeddedStruct. This example showcases how struct embedding can be used to implement an interface and inherit methods from an embedded struct.

package main

import "fmt"

type Worker interface {
   Work()
}

type EmbeddedStruct struct{}

func (e EmbeddedStruct) Work() {
   fmt.Println("EmbeddedStruct is working...")
}

type OuterStruct struct {
   EmbeddedStruct
   // Other fields specific to OuterStruct
}

func main() {
   outer := OuterStruct{}

   outer.Work() // Output: EmbeddedStruct is working...
}

Output

EmbeddedStruct is working...

Exampl 3

This code demonstrates Interface Assertion in Go. It begins by declaring an interface named Worker with a Work() method. Two types, Manager and Engineer, are implemented with their own Work() method implementations. In the main() function, instances of Manager and Engineer are created and assigned to variables of type Worker through interface assertion.

package main

import "fmt"

type Worker interface {
   Work()
}

type Manager struct{}

func (m Manager) Work() {
   fmt.Println("Manager is working...")
}

type Engineer struct{}

func (e Engineer) Work() {
   fmt.Println("Engineer is working...")
}

func main() {
   manager := Manager{}
   engineer := Engineer{}

   var worker1 Worker = manager
   var worker2 Worker = engineer

   if value, ok := worker1.(Manager); ok {
      value.Work()
   }

   if value, ok := worker2.(Engineer); ok {
      value.Work()
   }
}

Output

Manager is working...
Engineer is working...

Conclusion

In this article, we looked how to form an interface named "Specialist" with a "Work" strategy in Go. By characterizing an interface and executing it with distinctive struct sorts, we empower polymorphic behavior, permitting objects of distinctive sorts to be traded. The Laborer interface gives a contract that guarantees any sort actualizing it'll have a "Work" strategy. This adaptability permits us to compose more measured and extensible code.

Updated on: 20-Jul-2023

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements