liveBook · Manning

Slices

As explained in Learn Go with Pocket-Sized Projects

slices is Go's standard-library package providing generic operations for slice values.

The slices package provides reusable generic operations for Go slice values, including equality checks, membership tests, and element removal. It is useful when ordinary slice manipulation would otherwise require repeated, use-case-specific functions. section 5.6

A bounded generic cache uses slices to maintain insertion order:

  • Store keys in chronologicalKeys, initialized with a capacity matching the cache limit. section 7.4.2
  • Use slices.DeleteFunc to remove an updated, deleted, or expired key from the middle of the ordering. section 7.4.2
  • Append updated keys at the end and remove the oldest key when the cache reaches its maximum size. section 7.4.2

Removing a cache key

c.chronologicalKeys = slices.DeleteFunc(c.chronologicalKeys, func(k K) bool { return k == key }) removes matching keys while the associated map entry is deleted separately.

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains practical deletion, appending, ordering, and capacity allocation through a generic cache implementation.
Examples
Uses chronological cache keys, slices.DeleteFunc, maximum-size eviction, and a test covering updates and oldest-item removal.
What you can do after
Apply it via Go's slices package to compare, search, and remove elements in reusable slice-based code.
How it connects
Connects slices to generics, maps, FIFO ordering, cache eviction, capacity, and memory reallocation.

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.