| 1 |
// compile |
| 2 |
|
| 3 |
// Copyright 2022 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
type Cache[E comparable] struct { |
| 10 |
adder func(...E) |
| 11 |
} |
| 12 |
|
| 13 |
func New[E comparable]() *Cache[E] { |
| 14 |
c := &Cache[E]{} |
| 15 |
|
| 16 |
c.adder = func(elements ...E) { |
| 17 |
for _, value := range elements { |
| 18 |
value := value |
| 19 |
go func() { |
| 20 |
println(value) |
| 21 |
}() |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
return c |
| 26 |
} |
| 27 |
|
| 28 |
func main() { |
| 29 |
c := New[string]() |
| 30 |
c.adder("test") |
| 31 |
} |