Menu
Coddy logo textTech

Implementing an Interface

Part of the Logic & Flow section of Coddy's GO journey — lesson 14 of 68.

Now that you know how to define an interface, let's learn how Go types actually implement them. The most important thing to understand is that Go uses implicit implementation - there's no special keyword or declaration needed.

If a type has all the methods that an interface requires, it automatically satisfies that interface. You don't need to explicitly state that your type implements the interface - Go figures this out for you.

Here's how it works with the Shaper interface from the previous lesson:

type Circle struct {
    Radius float64
}

func (c Circle) Area() float64 {
    return 3.14159 * c.Radius * c.Radius
}

Since the Circle type has an Area() method that returns a float64, it automatically implements the Shaper interface. There's no need to declare this relationship explicitly - Go recognizes it based on the method signature match.

This implicit approach makes Go interfaces incredibly flexible. Any existing type can satisfy a new interface as long as it has the required methods, even if the interface was created after the type was written.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

In this challenge, you'll practice implementing an interface by creating a simple vehicle system. You'll create a Car struct that automatically implements a Vehicle interface through Go's implicit implementation.

You will receive two inputs:

  • A string representing the car's brand (e.g., "Toyota")
  • A string representing the car's speed in km/h (e.g., "120")

Your task is to:

  1. Define a Vehicle interface with one method signature: GetInfo() that returns a string
  2. Define a Car struct with two fields: Brand (string) and Speed (int)
  3. Implement the GetInfo() method for the Car struct that returns a string in the format: "Brand: [brand], Speed: [speed] km/h"
  4. Parse the inputs to create a Car instance
  5. Call the GetInfo() method and print the result

Remember that in Go, the Car struct will automatically implement the Vehicle interface because it has the required GetInfo() method with the correct signature. There's no need to explicitly declare that Car implements Vehicle - Go recognizes this relationship implicitly.

Cheat sheet

Go uses implicit implementation for interfaces - no special keyword or declaration is needed.

If a type has all the methods that an interface requires, it automatically satisfies that interface:

type Circle struct {
    Radius float64
}

func (c Circle) Area() float64 {
    return 3.14159 * c.Radius * c.Radius
}

Since Circle has an Area() method that returns a float64, it automatically implements the Shaper interface without explicit declaration.

This implicit approach makes Go interfaces flexible - any existing type can satisfy a new interface as long as it has the required methods, even if the interface was created after the type.

Try it yourself

package main

import (
	"fmt"
	"strconv"
)

// TODO: 1. Define the Vehicle interface with a GetInfo() string method

// TODO: 2. Define the Car struct with Brand (string) and Speed (int) fields

// TODO: 3. Implement the GetInfo() method for Car

func main() {
	// Read input
	var brand string
	var speedStr string
	fmt.Scanln(&brand)
	fmt.Scanln(&speedStr)

	// Convert speed string to integer
	speed, _ := strconv.Atoi(speedStr)

	// TODO: 4. Create a Car instance using brand and speed, then call GetInfo()
	result := "" // replace this line with your implementation
	_ = brand
	_ = speed

	// Output the result
	fmt.Println(result)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow