Skip to content
Learn Netverks

Lesson

Step 30/36 83% through track

interfaces-design

Interface design

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
advanced

This lesson

This lesson teaches Interface design: the syntax, patterns, and safety habits you need before advancing in Go.

Implicit interfaces enable small, testable packages—composition over inheritance is idiomatic Go.

You will apply Interface design in contexts like: Kubernetes ecosystem tools, cloud APIs, and CLI utilities.

Write Go in main.go with package main and func main(), click Run on server—the dev runner runs go run main.go; use fmt.Println for output (requires Go toolchain; LEARNING_RUNNER_ENABLED=true).

When pointers, structs, and basic control flow from intermediate lessons are familiar.

Go proverbs: "Accept interfaces, return structs." and "The bigger the interface, the weaker the abstraction." Design small interfaces at consumer side—io.Reader, io.Writer are canonical examples.

Consumer-driven interfaces

Define interfaces where they are used, not where types are implemented—keeps packages decoupled and tests easy with fakes.

Testing with fakes

type Storage interface {
    Get(key string) (string, error)
}

Implement fake storage in tests without touching production DB code—similar to mocking in Java but via simple structs.

Important interview questions and answers

  1. Q: Accept interfaces, return structs?
    A: APIs accept minimal interfaces for flexibility; return concrete types so callers know what they get.
  2. Q: io.Reader size?
    A: One method—easy to implement, compose, and mock.

Self-check

  1. Where should you define a small interface?
  2. Why avoid large interfaces?

Tip: "Accept interfaces, return structs"—return concrete types from constructors, accept minimal interfaces in function params.

Interview prep

Accept interfaces return structs?

Flexible inputs, concrete outputs—callers know what they receive.

Small interfaces?

io.Reader has one method—easy to implement, compose, and mock.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Accept interfaces?
  • Small interfaces?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump