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.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyIn 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:
- Define a
Vehicleinterface with one method signature:GetInfo()that returns a string - Define a
Carstruct with two fields:Brand(string) andSpeed(int) - Implement the
GetInfo()method for theCarstruct that returns a string in the format:"Brand: [brand], Speed: [speed] km/h" - Parse the inputs to create a
Carinstance - 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)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Control Flow
Switch with `fallthrough`Breaking from Nested LoopsContinuing a Specific LoopThe `goto` StatementRecap - Advanced Loop Control4Project: Simple Task List
Project SetupAdding a Task2Structs and Methods
Defining Methods on StructsValue ReceiversPointer ReceiversChoosing ReceiversMethods vs FunctionsRecap - Struct Behavior5Maps In-Depth
Maps of StructsPointers as Map ValuesTesting for Nil MapsComparing MapsRecap - Word Frequency Counter3Interfaces (The Basics)
What is an Interface?Defining an InterfaceImplementing an InterfaceUsing Interface TypesEmpty InterfaceType AssertionsType SwitchRecap - Shapes and Behaviors