Source file
test/typeparam/issue50109.go
1
2
3
4
5
6
7 package main
8
9 import (
10 "fmt"
11 )
12
13 type AnyCacher[T any] interface {
14
15
16 Get(k string) (T, bool)
17
18 Set(k string, x T)
19 }
20
21
22 type Item[T any] struct {
23 Object T
24 }
25
26
27 type AnyCache[T any] struct {
28 *anyCache[T]
29 }
30
31 type anyCache[T any] struct {
32 items map[string]Item[T]
33 janitor *janitor[T]
34 }
35
36
37 func (c *anyCache[T]) Set(k string, x T) {
38 c.items[k] = Item[T]{
39 Object: x,
40 }
41 }
42
43
44
45 func (c *anyCache[T]) Get(k string) (T, bool) {
46
47 item, found := c.items[k]
48 if !found {
49 var ret T
50 return ret, false
51 }
52
53 return item.Object, true
54 }
55
56 type janitor[T any] struct {
57 stop chan bool
58 }
59
60 func newAnyCache[T any](m map[string]Item[T]) *anyCache[T] {
61 c := &anyCache[T]{
62 items: m,
63 }
64 return c
65 }
66
67
68 func NewAny[T any]() *AnyCache[T] {
69 items := make(map[string]Item[T])
70 return &AnyCache[T]{newAnyCache(items)}
71 }
72
73
74 func NewAnyCacher[T any]() AnyCacher[T] {
75 return NewAny[T]()
76 }
77
78 type MyStruct struct {
79 Name string
80 }
81
82 func main() {
83
84
85
86
87 c := NewAnyCacher[any]()
88
89 myStruct := &MyStruct{"MySuperStruct"}
90
91 c.Set("MySuperStruct", myStruct)
92
93 myRawCachedStruct, found := c.Get("MySuperStruct")
94
95 if found {
96
97 myCachedStruct := myRawCachedStruct.(*MyStruct)
98 fmt.Printf("%s", myCachedStruct.Name)
99 } else {
100 fmt.Printf("Error: MySuperStruct not found in cache")
101 }
102
103
104
105 }
106
View as plain text