SoFunction
Updated on 2025-03-05

Detailed explanation of the examples of using Go language learning interfaces

As mentioned earlier, Go language does not have a class definition. The interface can be said that Go language is closest to the implementation method of class, but it is lighter. For the learning of interfaces, if it is slow to understand from the principle level, it is recommended to first understand the code using dimensions, and finally return to the principle level to deepen the understanding.

Requirements and Analysis

Suppose we have a set of graphs that need to calculate the area of ​​each graph and calculate the sum of their areas. Then the easiest way is to calculate their areas separately and add them together. Let's try to implement it.

Implementation without using interfaces

In this code implementation, we need to sum the areas of two different shapes, rect and circle, so we define the following:

  • Two structures, rectangle is length and width, circle is radius
  • Two methods of area () are implemented separately. The area of ​​the rectangle is equal to the length multiplied by the width, while the area of ​​the circle is the square of the radius multiplied by Pi
  • In the summing part, we directly define an array of float64 and store the area directly into the array
  • Sum through loop

Although the above method can meet our needs, if we need to add a method to calculate the perimeter, our code will become very redundant and poorly readable, so we try to remodel our code with interfaces.

package main

import (
    "fmt"
    "math"
)

type rect struct {
    width float64
    height float64
}

func (r rect) area() float64 {
    return  * 
}

type circle struct {
    radius float64
}

func (c circle) area() float64 {
    return  *  * 
}

func main() {
    var areaSum float64
    
    // Intial circle and rect struct type
    c1 := circle{2.5}
    r1 := rect{3, 4}
    
    // Save all area results into an array
    shapeAreas := []float64{(), ()}
    
    // Sum all area together
    areaSum = 0
    for _, area := range shapeAreas {
        areaSum += area
    }
    
    ("Sum area = %v\n", areaSum)
}

Implementation of using interfaces

Compared with the above code, we have made the following optimizations:

  • Define a new interface shape, including an area() method, that is, implement the struct of area(), and implement the shape interface
  • In the structure definition, we did not modify the area() calculation part.
  • In the main function, we redefine an array of type shape interface. There is no need to calculate area() in this array, we only need to store two unpassable types in the array
  • During the loop, we directly call the area() method in each shape interface to complete the area summation
package main

import (
    "fmt"
    "math"
)

// Define a new interface, contain a method define and type is float64
type shape interface {
    area() float64
}

type rect struct {
    width float64
    height float64
}

func (r rect) area() float64 {
    return  * 
}

type circle struct {
    radius float64
}

func (c circle) area() float64 {
    return  *  * 
}

func main() {
    var areaSum float64
    
    // Intial circle and rect struct type
    c1 := circle{2.5}
    r1 := rect{3, 4}
    
    // Previous: Save all area results into an array
    // Previous: shapeAreas := []float64{(), ()}
    
    // Define an array with new shape interface
    shapes := []shape{c1, r1}
    
    // Previous: Sum all area together
    areaSum = 0
    // Previous: for _, area := range shapeAreas {
    // Previous:     areaSum += area
    // Previous: }
    
    // Implement a new loop
    for _, shape := range shapes {
        areaSum += ()
    }
    
    ("Sum area = %v\n", areaSum)
}

Interface as function parameter

To further optimize the code, we use the interface as a parameter. When calling it in the main function, we only need to pass in the corresponding type and automatically call the corresponding method of calculating area according to the type.

package main

import (
    "fmt"
    "math"
)

// Define a new interface, contain a method define and type is float64
type shape interface {
    area() float64
}

type rect struct {
    width float64
    height float64
}

// NOTE: The interface type is rectfunc (r rect) area() float64 {
    return  * 
}

type circle struct {
    radius float64
}

// NOTE: The interface type is circlefunc (c circle) area() float64 {
    return  *  * 
}

func getArea(s shape) float64 {
    return ()
}

func main() {
    var areaSum float64

    // Intial circle and rect struct type
    c1 := circle{2.5}
    r1 := rect{3, 4}

    // Previous: Save all area results into an array
    // Previous: shapeAreas := []float64{(), ()}

    // Define an array with new shape interface
    shapes := []shape{c1, r1}

    // Previous: Sum all area together
    areaSum = 0
    // Previous: for _, area := range shapeAreas {
    // Previous:     areaSum += area
    // Previous: }

    // Implement a new loop
    for _, shape := range shapes {
        areaSum += getArea(shape)
    }

    ("Sum area = %v\n", areaSum)
}

This is the end of this article about the detailed explanation of the examples of the use of Go language learning interfaces. For more related Go language interface content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!