Go interfaces

In the first blog of the Go programming series, I introduced interfaces. This time, let’s dive deeper into the topic.

1. Definition and Usage of Interfaces

What is an Interface?

In Go, an interface is a type that specifies a set of method signatures. When a type (usually a struct) implements all the methods defined by an interface, it is said to satisfy that interface. 

Defining an Interface

To define an interface, you declare it with the type keyword, followed by the interface name and the keyword interface. Inside the interface, you define one or more method signatures. 

2. Polymorphism with Interfaces

What is Polymorphism?

Polymorphism is the ability of different types to be used interchangeably through a common interface. This allows for more flexible and reusable code.

Achieving Polymorphism with Interfaces

In Go, polymorphism is achieved using interfaces. A function can accept parameters of an interface type, allowing it to work with any value that satisfies that interface. Here’s an example:

package main

import "fmt"

// Define the Shape interface with a method signature for Area

type Shape interface {

  Area() float64

}

// Define a Circle type

type Circle struct {

  Radius float64

}

// Define a Rectangle type

type Rectangle struct {

  Width, Height float64

}

// Implement the Area method for Circle

func (c Circle) Area() float64 {

  return 3.14 * c.Radius * c.Radius

}

// Implement the Area method for Rectangle

func (r Rectangle) Area() float64 {

  return r.Width * r.Height

}

// Function that takes a Shape interface and prints its area

func ComputeArea(s Shape) {

  fmt.Printf("The area is: %.2f\n", s.Area())

}

func main() {

  c := Circle{Radius: 5}

  r := Rectangle{Width: 4, Height: 6}

  ComputeArea(c) // Output: The area is: 78.50

  ComputeArea(r) // Output: The area is: 24.00

}

What does the code do?

– The Shape interface defines a method Area() that any shape must implement.

– Both Circle and Rectangle types implement the Area() method, satisfying the Shape interface.

– The ComputeArea function can take any type that implements the Shape interface, demonstrating polymorphism: different types (Circle, Rectangle) are used interchangeably through the common Shape interface. This makes the code more flexible and reusable, as you can easily add new shapes by implementing the Shape interface.

3. Empty Interface

What is an Empty Interface?

The empty interface is a special type in Go represented as interface{}. It does not contain any methods, which means all types in Go satisfy the empty interface.

package main

import "fmt"

func printValueType(i interface{}) {

  fmt.Printf("(%v, %T)\n", i, i)

}

func main() {

  printValueType("Hello world") // Output: Hello world, string

  printValueType(10) // Output: 10, int

  printValueType(3.14) // Output: 3.14, float64

  printValueType(true) // Output: true, bool

}

 

Here are some important points I would like to share in this section:

–  Avoid Using the Empty Interface as Much as Possible

Go is a statically typed programming language, which means that the type of variables is known at compile time. Using types properly in Go can significantly improve the performance, readability, and maintainability of your code. Let’s explore why types are important in Go and how to use them effectively.

– Drawbacks of the Empty Interface

When you use an empty interface, you lose compile-time type checking. This means you can’t be sure of a variable’s type, which can lead to potential runtime errors if the type doesn’t match what you expect.

Using the empty interface can make code harder to understand and maintain because it becomes unclear what types are being passed around. This reduces readability and can lead to confusion, especially in larger codebases.

Working with the empty interface can introduce a slight performance overhead due to the need for type assertions and type switches. 

In summary, Interfaces in Go are essential for achieving flexibility and maintainability. By defining behavior through interfaces, you can create scalable and testable code. Use interfaces wisely, and avoid overusing the empty interface to maintain type safety and clarity. Embrace interfaces to build robust and clean Go applications.

関連記事

カテゴリー:

ブログ

情シス求人

  1. 登録されている記事はございません。
ページ上部へ戻る