Defining an Interface
Part of the Logic & Flow section of Coddy's GO journey — lesson 13 of 68.
Now that you understand what interfaces are conceptually, let's learn how to actually define one in Go. An interface is defined using the interface keyword followed by a set of method signatures enclosed in curly braces.
Here's the basic syntax for defining an interface:
type InterfaceName interface {
MethodName() ReturnType
AnotherMethod(parameter Type) ReturnType
}Notice that an interface only contains method signatures - the method name, parameters, and return types - but no implementation. The interface doesn't specify how these methods should work, only what methods must exist.
For example, here's how you would define a simple Shaper interface that requires any implementing type to have an Area method:
type Shaper interface {
Area() float64
}This interface establishes a contract: any type that wants to be considered a Shaper must have an Area method that takes no parameters and returns a float64. The interface doesn't care what the type is or how it calculates the area - it only cares that the method exists with the correct signature.
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 defining interfaces in Go by creating a simple media player system. You'll define an interface that establishes a contract for different types of media devices.
You will receive one input:
- A string representing a media action (e.g.,
"play","pause", or"stop")
Your task is to:
- Define an interface called
MediaPlayerwith three method signatures:Play()that returns a stringPause()that returns a stringStop()that returns a string
- Based on the input action, print the corresponding method signature from your
MediaPlayerinterface in the format:"MediaPlayer interface requires: [MethodName]() string"
The print statement is meant to reflect on the interface you defined above — you are describing which method signature the MediaPlayer interface requires for the given action. Think of it as your program inspecting and reporting on its own interface contract.
The method names should be capitalized (exported) as they are part of an interface definition. For the input "play", print "MediaPlayer interface requires: Play() string", for "pause" print "MediaPlayer interface requires: Pause() string", and for "stop" print "MediaPlayer interface requires: Stop() string".
This challenge focuses on the fundamental concept of defining interfaces with method signatures, establishing the contract that any implementing type must fulfill.
Cheat sheet
An interface is defined using the interface keyword followed by method signatures in curly braces:
type InterfaceName interface {
MethodName() ReturnType
AnotherMethod(parameter Type) ReturnType
}Interfaces contain only method signatures (name, parameters, return types) but no implementation:
type Shaper interface {
Area() float64
}Any type that implements all the methods in an interface automatically satisfies that interface contract.
Try it yourself
package main
import "fmt"
func main() {
// Read input
var action string
fmt.Scanln(&action)
// TODO: Define your MediaPlayer interface here
// TODO: Write your code below to handle the action and print the required output
// Print the result
// Remember to print in format: "MediaPlayer interface requires: [MethodName]() string"
}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