Bird
Raised Fist0
Goprogramming~20 mins

Interface satisfaction in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Go program using interface satisfaction?
Consider the following Go code. What will it print when run?
Go
package main

import "fmt"

type Speaker interface {
	Speak() string
}

type Dog struct {}

func (d Dog) Speak() string {
	return "Woof!"
}

type Cat struct {}

func (c Cat) Speak() string {
	return "Meow!"
}

func saySomething(s Speaker) {
	fmt.Println(s.Speak())
}

func main() {
	d := Dog{}
	c := Cat{}

	saySomething(d)
	saySomething(c)
}
AWoof!\nWoof!
BWoof!\nMeow!
CMeow!\nWoof!
DCompilation error: Dog does not implement Speaker
Attempts:
2 left
💡 Hint
Check which types implement the Speak() method of the Speaker interface.
❓ Predict Output
intermediate
2:00remaining
What error does this Go code produce regarding interface satisfaction?
Look at this Go code snippet. What error will it produce when compiled?
Go
package main

import "fmt"

type Reader interface {
	Read(p []byte) (n int, err error)
}

type MyReader struct {}

func (r *MyReader) Read(p []byte) (int, error) {
	return 0, nil
}

func main() {
	var r Reader = MyReader{}
	fmt.Println(r)
}
Acannot use MyReader{} (type MyReader) as type Reader in assignment: MyReader does not implement Reader (Read method has pointer receiver)
Bnil
CCompilation error: missing Read method
DProgram runs and prints: {}
Attempts:
2 left
💡 Hint
Check if the method receiver type matches the interface implementation.
🔧 Debug
advanced
2:00remaining
Why does this Go program panic at runtime when using interface satisfaction?
Examine the code below. It compiles but panics at runtime. Why?
Go
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
	p.Print()
}
Ap is nil interface, calling Print() causes runtime panic: nil pointer dereference
BCompilation error: Printer interface not implemented
CProgram prints empty line
DProgram prints zero value of Document
Attempts:
2 left
💡 Hint
What is the value of p before calling Print()?
📝 Syntax
advanced
2:00remaining
Which option correctly implements interface satisfaction with embedded interfaces?
Given these interfaces, which implementation satisfies the Composite interface?
Go
package main

type Reader interface {
	Read(p []byte) (n int, err error)
}

type Writer interface {
	Write(p []byte) (n int, err error)
}

type Composite interface {
	Reader
	Writer
}

// Which struct and methods satisfy Composite?
Atype RW struct{}\nfunc (rw *RW) Write(p []byte) (int, error) { return 0, nil }
Btype RW struct{}\nfunc (rw *RW) Read(p []byte) (int, error) { return 0, nil }\nfunc (rw RW) Write(p []byte) (int, error) { return 0, nil }
Ctype RW struct{}\nfunc (rw RW) Read(p []byte) (int, error) { return 0, nil }
Dtype RW struct{}\nfunc (rw RW) Read(p []byte) (int, error) { return 0, nil }\nfunc (rw RW) Write(p []byte) (int, error) { return 0, nil }
Attempts:
2 left
💡 Hint
Both Read and Write methods must be implemented with matching receiver types.
🚀 Application
expert
2:00remaining
How many items are in the slice after interface satisfaction and type assertion?
What is the length of the slice 'speakers' after the code runs?
Go
package main

import "fmt"

type Speaker interface {
	Speak() string
}

type Human struct {
	name string
}

func (h Human) Speak() string {
	return "Hello, " + h.name
}

func main() {
	speakers := []Speaker{}

	h1 := Human{"Alice"}
	h2 := Human{"Bob"}

	speakers = append(speakers, h1)

	var s Speaker = h2
	if h, ok := s.(Human); ok {
		speakers = append(speakers, h)
	}

	fmt.Println(len(speakers))
}
A0
B1
C2
DCompilation error
Attempts:
2 left
💡 Hint
Check how many times append is called and what is appended.

Practice

(1/5)
1. Which statement best describes how a type satisfies an interface in Go?
easy
A. A type satisfies an interface by implementing all the methods declared in the interface.
B. A type satisfies an interface only if it explicitly declares it implements the interface.
C. A type satisfies an interface by embedding the interface in its struct definition.
D. A type satisfies an interface by having the same name as the interface.

Solution

  1. Step 1: Understand interface satisfaction in Go

    In Go, a type satisfies an interface if it implements all the methods declared in that interface.
  2. Step 2: Check explicit declaration requirement

    Go does not require explicit declaration; satisfaction is implicit by method implementation.
  3. Final Answer:

    A type satisfies an interface by implementing all the methods declared in the interface. -> Option A
  4. Quick Check:

    Interface satisfaction = implicit method implementation [OK]
Hint: Remember: Implement all methods, no explicit declaration needed [OK]
Common Mistakes:
  • Thinking explicit declaration is required
  • Confusing embedding with satisfaction
  • Matching names instead of methods
2. Which of the following Go code snippets correctly shows a type satisfying an interface Speaker with method Speak()?
easy
A. type Speaker interface { Speak() string } type Dog struct {} func (d Dog) speak() string { return "Woof" }
B. type Speaker interface { Speak() string } type Dog struct {} func (d Dog) Speak() string { return "Woof" }
C. type Speaker interface { Speak() string } type Dog struct {} func Speak(d Dog) string { return "Woof" }
D. type Speaker interface { Speak() string } type Dog struct {} func (d *Dog) Speak() int { return 1 }

Solution

  1. Step 1: Check method signature and receiver

    type Speaker interface { Speak() string } type Dog struct {} func (d Dog) Speak() string { return "Woof" } defines Speak() with correct receiver (d Dog) and return type string.
  2. Step 2: Verify method name and return type match interface

    type Speaker interface { Speak() string } type Dog struct {} func (d Dog) speak() string { return "Woof" } has lowercase speak(), so it does not satisfy interface. type Speaker interface { Speak() string } type Dog struct {} func Speak(d Dog) string { return "Woof" } defines a function, not a method. type Speaker interface { Speak() string } type Dog struct {} func (d *Dog) Speak() int { return 1 } has wrong return type int.
  3. Final Answer:

    type Speaker interface { Speak() string } type Dog struct {} func (d Dog) Speak() string { return "Woof" } -> Option B
  4. Quick Check:

    Method name and signature must match exactly [OK]
Hint: Method name and signature must match interface exactly [OK]
Common Mistakes:
  • Using lowercase method names
  • Defining functions instead of methods
  • Mismatching return types
3. What will be the output of the following Go program?
package main
import "fmt"

type Reader interface {
  Read() string
}

type File struct {}
func (f File) Read() string {
  return "file content"
}

func printContent(r Reader) {
  fmt.Println(r.Read())
}

func main() {
  f := File{}
  printContent(f)
}
medium
A. No output
B. Compilation error: File does not satisfy Reader
C. file content
D. Runtime panic

Solution

  1. Step 1: Check if File satisfies Reader interface

    File has method Read() string, so it satisfies Reader.
  2. Step 2: Trace function call and output

    In main, f is passed to printContent, which calls Read() and prints "file content".
  3. Final Answer:

    file content -> Option C
  4. Quick Check:

    Interface satisfied, method called, prints string [OK]
Hint: Check method presence to confirm interface satisfaction [OK]
Common Mistakes:
  • Assuming explicit declaration needed
  • Confusing method receiver types
  • Expecting runtime errors without cause
4. Identify the error in the following Go code that prevents Car from satisfying the Vehicle interface:
package main
import "fmt"

type Vehicle interface {
  Drive()
}

type Car struct {}
func (c *Car) Drive() {
  fmt.Println("Driving")
}

func main() {
  var v Vehicle
  c := Car{}
  v = c
  v.Drive()
}
medium
A. Car does not satisfy Vehicle because Drive has pointer receiver but v is assigned a value.
B. Drive method has wrong return type.
C. Vehicle interface requires Drive with parameters.
D. No error, code runs fine.

Solution

  1. Step 1: Check method receiver type

    Drive is defined with pointer receiver (*Car), so only *Car satisfies Vehicle, not Car value.
  2. Step 2: Check assignment in main

    Variable v is assigned c which is a value, not pointer, causing mismatch.
  3. Final Answer:

    Car does not satisfy Vehicle because Drive has pointer receiver but v is assigned a value. -> Option A
  4. Quick Check:

    Pointer receiver methods require pointer type for interface satisfaction [OK]
Hint: Pointer receiver methods need pointer type to satisfy interface [OK]
Common Mistakes:
  • Assigning value instead of pointer to interface
  • Ignoring receiver type in method
  • Assuming method signature mismatch
5. Given the interface and types below, which assignment will cause a compile-time error due to interface satisfaction rules?
type Writer interface {
  Write(data string) error
}

type Logger struct {}
func (l Logger) Write(data string) error {
  return nil
}

type FileWriter struct {}
func (f *FileWriter) Write(data string) error {
  return nil
}

var w Writer

// Which assignment is invalid?
// A) w = Logger{}
// B) w = &Logger{}
// C) w = FileWriter{}
// D) w = &FileWriter{}
hard
A. w = &FileWriter{}
B. w = &Logger{}
C. w = Logger{}
D. w = FileWriter{}

Solution

  1. Step 1: Analyze Logger methods

    Logger has Write with value receiver, so both Logger and *Logger satisfy Writer.
  2. Step 2: Analyze FileWriter methods

    FileWriter has Write with pointer receiver, so only *FileWriter satisfies Writer, not FileWriter value.
  3. Step 3: Check assignments

    Assignment C tries to assign FileWriter{} value to Writer, which is invalid.
  4. Final Answer:

    w = FileWriter{} causes compile-time error due to missing method on value receiver. -> Option D
  5. Quick Check:

    Pointer receiver methods require pointer type for interface [OK]
Hint: Pointer receiver methods need pointer type for interface satisfaction [OK]
Common Mistakes:
  • Assigning value type when method has pointer receiver
  • Assuming pointer and value types are interchangeable
  • Ignoring method receiver type
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard