Open 981377660LMT opened 2 months ago
可以看到,带波浪线和不带波浪线的类型是不一样的
建议:用波浪线(tilde)
In Go generics, the ~ tilde token is used in the form ~T to denote the set of types whose underlying type is T. 在 Go 泛型中, ~波形符标记以~T形式使用,表示基础类型为T的类型集。
It was also called "approximation" constraint element in the generics proposal, which explains what it's good for in plain language: 它在泛型提案中也被称为“近似”约束元素,它用简单的语言解释了它的好处:
Listing a single type is useless by itself. For constraint satisfaction, we want to be able to say not just int, but “any type whose underlying type is int”. [...] If a program uses type MyString string, the program can use the < operator with values of type MyString. It should be possible to instantiate [a function] with the type MyString. 列出单一类型本身是没有用的。为了满足约束条件,我们不仅希望能够说 int,还希望能够说“基础类型为 int 的任何类型”。 [...] 如果程序使用type MyString string ,则该程序可以将<运算符与MyString类型的值一起使用。应该可以使用MyString类型实例化[函数]。
golang里写泛型最好带波浪线,不带的话,不支持自定义类型
~T means the set of all types with underlying type T ~T 表示具有基础类型 T 的所有类型的集合