📖 Scenario: You are building a simple program to represent different types of vehicles. Each vehicle can describe itself by returning its type as a string.
🎯 Goal: Create an interface called Vehicle with a method Type() that returns a string. Then create two structs Car and Bike that implement this interface.
📋 What You'll Learn
Create an interface named Vehicle with a method Type() string
Create a struct named Car
Create a struct named Bike
Implement the Type() method for both Car and Bike to return their type as a string
Create variables of type Car and Bike
Use the Vehicle interface to call the Type() method on both variables
Print the results
💡 Why This Matters
🌍 Real World
Interfaces let you write code that works with different types in a uniform way, like handling different vehicles in a transport app.
💼 Career
Understanding interfaces is key for Go developers to build modular, testable, and maintainable software.
Progress0 / 4 steps
1
Create structs for vehicles
Create two structs called Car and Bike with no fields.
Go
Hint
Use the type keyword to define structs with empty braces {}.
2
Define the Vehicle interface
Create an interface called Vehicle with a method Type() string.
Go
Hint
Use type Vehicle interface { Type() string } to define the interface.
3
Implement the Type() method for Car and Bike
Write the Type() method for Car that returns "Car" and for Bike that returns "Bike".
Go
Hint
Define methods with receiver types Car and Bike that return the correct string.
4
Use the Vehicle interface and print results
Create variables myCar of type Car and myBike of type Bike. Then create variables v1 and v2 of type Vehicle assigned to myCar and myBike. Finally, print the results of calling Type() on v1 and v2.
Go
Hint
Use var v1 Vehicle = myCar to assign the struct to the interface variable. Use println() to print the results.
Practice
(1/5)
1. What does an interface in Go define?
easy
A. A concrete type with fields and methods
B. A variable that holds data
C. A set of method signatures without implementation
D. A function that returns multiple values
Solution
Step 1: Understand what an interface is
An interface in Go specifies method signatures but does not provide their implementation.
Step 2: Differentiate from other types
Concrete types have fields and method implementations, unlike interfaces.
Final Answer:
A set of method signatures without implementation -> Option C
Quick Check:
Interface = method signatures only [OK]
Hint: Interfaces list methods only, no code inside [OK]
Common Mistakes:
Thinking interfaces hold data
Confusing interfaces with structs
Assuming interfaces implement methods
2. Which of the following is the correct syntax to define an interface named Shape with a method Area() returning a float64?
easy
A. func Shape interface { Area() float64 }
B. interface Shape { func Area() float64 }
C. type Shape struct { Area() float64 }
D. type Shape interface { Area() float64 }
Solution
Step 1: Recall Go interface syntax
Interfaces are defined with the keyword type, name, then interface and method signatures inside braces.
Step 2: Check each option
type Shape interface { Area() float64 } matches correct syntax. Options B, C, and D misuse keywords or structure.
Final Answer:
type Shape interface { Area() float64 } -> Option D
Hint: Use 'type Name interface { methods }' for interfaces [OK]
Common Mistakes:
Using 'interface' keyword alone without 'type'
Defining methods inside struct instead of interface
Placing 'func' keyword inside interface method
3. What will be the output of this Go code?
package main
import "fmt"
type Printer interface {
Print()
}
type Document struct {
content string
}
func (d Document) Print() {
fmt.Println(d.content)
}
func main() {
var p Printer
d := Document{"Hello, Go!"}
p = d
p.Print()
}
medium
A. Runtime panic: nil pointer dereference
B. Hello, Go!
C. Compilation error: cannot assign Document to Printer
D. No output
Solution
Step 1: Check interface implementation
Document has a method Print() matching Printer interface, so Document implements Printer.
Step 2: Assign and call method
Variable p of type Printer is assigned d (Document), then p.Print() calls Document.Print(), printing content.
Final Answer:
Hello, Go! -> Option B
Quick Check:
Interface method calls concrete method [OK]
Hint: Assign concrete type with matching methods to interface [OK]