go 实现泛型
Go has strong, static types and it does not support generics, so how can we define general purpose data structures and algorithms that can be applied to more than one type? interface{}
s are not the solution, as they require casting and we lose a lot of the advantages of having strong, static typing. The solution is code generation, as it allows to get compile-time checks and safety, and higher performance.
Go具有强大的静态类型,并且不支持泛型,那么我们如何定义可应用于多种类型的通用数据结构和算法? interface{}
并不是解决方案,因为它们需要强制转换,而我们失去了拥有强大的静态类型的许多优点。 解决方案是代码生成 ,因为它可以获取编译时检查和安全性以及更高的性能。
I didn’t find this information easily accessible when searching for code generation topics, but I stumbled on more complex use cases and scenarios, so here it is, explained in plain english.
在搜索代码生成主题时,我发现这些信息不容易获得,但是我偶然发现了更复杂的用例和场景,因此这里用简单的英语进行了解释。
问题: (Problem:)
I want to implement a data structure (the same applies to an algorithm) in the most general possible way with Go, and generate type-specific implementations that I can easily reuse.
我想用Go 以最通用的方式 实现数据结构 (对算法也是如此),并生成可以轻松重用的特定于类型的实现 。
解: (Solution:)
Using genny, this couldn’t