type Work … func (w *Work[T]) init() { … } // Add adds item to the work set, if it hasn't already been added. func (w *Work[T]) Add(item T) { … } // Do runs f in parallel on items from the work set, // with at most n invocations of f running at a time. // It returns when everything added to the work set has been processed. // At least one item should have been added to the work set // before calling Do (or else Do returns immediately), // but it is allowed for f(item) to add new items to the set. // Do should only be used once on a given Work. func (w *Work[T]) Do(n int, f func(item T)) { … } // runner executes work in w until both nothing is left to do // and all the runners are waiting for work. // (Then all the runners return.) func (w *Work[T]) runner() { … } type ErrCache … type errValue … func (c *ErrCache[K, V]) Do(key K, f func() (V, error)) (V, error) { … } var ErrCacheEntryNotFound … // Get returns the cached result associated with key. // It returns ErrCacheEntryNotFound if there is no such result. func (c *ErrCache[K, V]) Get(key K) (V, error) { … } type Cache … type cacheEntry … // Do calls the function f if and only if Do is being called for the first time with this key. // No call to Do with a given key returns until the one call to f returns. // Do returns the value returned by the one call to f. func (c *Cache[K, V]) Do(key K, f func() V) V { … } // Get returns the cached result associated with key // and reports whether there is such a result. // // If the result for key is being computed, Get does not wait for the computation to finish. func (c *Cache[K, V]) Get(key K) (V, bool) { … }