liveBook · Manning

Empty interface

As explained in Build and Orchestrator in Go (From Scratch)

An empty interface is a Go interface type that imposes no method requirements and can hold a value of any type.

An empty interface is a Go interface type with no method requirements. A value of type interface{} can therefore hold a task.Task, task.TaskEvent, or another type, helping a manager depend on a storage contract instead of a built-in map. section 11.2

Using an empty interface shifts type checking to the store implementation:

  • Put accepts an arbitrary value, then uses a type assertion to require a pointer to task.Task before storing it. section 11.3
  • Get returns interface{}; callers use a type assertion to convert the result back to *task.Task and handle failures. section 11.3 section 11.5
  • List returns an interface{} containing a slice of tasks, which the manager converts to []*task.Task. section 11.3 section 11.5

A typed boundary around flexible storage

type Store interface { Put(key string, value interface{}) error Get(key string) (interface{}, error) List() (interface{}, error) Count() (int, error) } func (i *InMemoryTaskStore) Put(key string, value interface{}) error { t, ok := value.(*task.Task) if !ok { return fmt.Errorf("value is not a task.Task type") } i.Db[key] = t return nil }

Listing 11.2 The Store interface
type Store interface {
   Put(key string, value interface{}) error
   Get(key string) (interface{}, error)
   List() (interface{}, error)
   Count() (int, error)
}

Inspect the Store interface to see where empty-interface values make storage implementations interchangeable. section 11.2

How Build and Orchestrator in Go (From Scratch) covers it

How deep it goes
Explains flexible method signatures, concrete type assertions, comma-ok checks, and conversions, but stops short of broader type-system treatment.
Examples
Uses task.Task and task.TaskEvent values, Store methods, an in-memory map, and manager conversions from interface{}.
What it assumes
Assumes familiarity with Go interfaces, pointers, maps, and error returns; demonstrates type assertions through storage operations.
What you can do after
Apply it via Go type assertions to implement flexible Store boundaries and safely recover concrete task values.
How it connects
Connects empty interfaces to Go interfaces, type assertions, in-memory stores, maps, and datastore abstraction.

Also explained in

Compare how 4 books explain empty interface →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build and Orchestrator in Go (From Scratch). The section links point to the parts of the book it draws on.