How to implement interfaces from a different packages?

This example aims to demonstrate the implementation of interfaces in Go and import your custom package. You will be able to define and declare an interface for an application in custom packages and implement that interface in your applications.

The following will be the directory structure of our application.
├── analysis
│   ├── go.mod
│   ├── main.go
│   ├── vehicle
│   │   └── vehicle.go
│   └── human
│       └── human.go

Go inside the analysis directory and run the following command to create a go module named analysis.

go mod init analysis

The above command will create a file named go.mod. The following will be the contents of the file.

module analysis

go 1.14

analysis\main.go

To use a custom package we must import it first. The import path is the name of the module appended by the subdirectory of the package and the package name. In our example the module name is analysis and the package human is in the human folder directly under analysis folder. Similarly the package vehicle is in the vehicle folder directly under analysis folder.

Hence, the line import "analysis/vehicle" will import the vehicle package, and "analysis/human" will import the human package

package main

import (
	h "analysis/human"
	v "analysis/vehicle"

	"fmt"
)

func main() {
	var bmw v.Vehicle
	bmw = v.Car("World Top Brand")

	var labour h.Human
	labour = h.Man("Software Developer")

	for i, j := range bmw.Structure() {
		fmt.Printf("%-15s <=====> %15s\n", j, labour.Structure()[i])
	}
}

We are aliasing the vehicle package as v and human package as h. In the main() function, we assign bmw which is of type Vehicle. Since Car implements the Vehicle interface. bmw.Structure() calls the Structure method on Car type.


analysis\vehicle\vehicle.go

Create a file vehicle.go inside the vehicle folder. The file inside the vehicle folder should start with the line package vehicle as it belongs to the vehicle package.

package vehicle

type Vehicle interface {
	Structure() []string // Common Method
	Speed() string
}

type Car string

func (c Car) Structure() []string {
	var parts = []string{"ECU", "Engine", "Air Filters", "Wipers", "Gas Task"}
	return parts
}

func (c Car) Speed() string {
	return "200 Km/Hrs"
}

We added the method Structure() and Speed() to the receiver type Car. Now Car is said to implement the interface Vehicle.


analysis\human\human.go

Create a file human.go inside the human folder. The file inside the human folder should start with the line package human as it belongs to the human package.

package human

type Human interface {
	Structure() []string // Common Method
	Performance() string
}

type Man string

func (m Man) Structure() []string {
	var parts = []string{"Brain", "Heart", "Nose", "Eyelashes", "Stomach"}
	return parts
}

func (m Man) Performance() string {
	return "8 Hrs/Day"
}

We added the method Structure() and Performance() to the receiver type Man. Now Man is said to implement the interface Human.


analysis>go run main.go

If you run the program, you will get the following output.

ECU             <=====>           Brain
Engine          <=====>           Heart
Air Filters     <=====>            Nose
Wipers          <=====>       Eyelashes
Gas Task        <=====>         Stomach


Most Helpful This Week