File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
images/garbage_collection Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ # Generics
2+
3+ 在泛型出现之前,社区里也有一些伪泛型方案,例如 [ genny] ( https://github.com/cheekybits/genny ) 。
4+
5+ genny 本质是基于文本替换的,比如 example 里的例子:
6+
7+ ``` go
8+ package queue
9+
10+ import " github.com/cheekybits/genny/generic"
11+
12+ // NOTE: this is how easy it is to define a generic type
13+ type Something generic.Type
14+
15+ // SomethingQueue is a queue of Somethings.
16+ type SomethingQueue struct {
17+ items []Something
18+ }
19+
20+ func NewSomethingQueue () *SomethingQueue {
21+ return &SomethingQueue{items: make ([]Something, 0 )}
22+ }
23+ func (q *SomethingQueue ) Push (item Something ) {
24+ q.items = append (q.items , item)
25+ }
26+ func (q *SomethingQueue ) Pop () Something {
27+ item := q.items [0 ]
28+ q.items = q.items [1 :]
29+ return item
30+ }
31+ ```
32+
33+ 执行替换命令:
34+
35+ ``` shell
36+ cat source.go | genny gen " Something=string"
37+ ```
38+
39+ 然后 genny 会将代码中所有 Something 替换成 string,同时确保大小写与原来一致,不影响字段/类型的导出特征。
40+
41+ 没有官方的泛型支持,社区怎么搞都是邪道。2021 年 1 月,官方的方案已经基本上成型,并释出了 [ draft design] ( https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md ) 。
42+
You can’t perform that action at this time.
0 commit comments