小型网站开发 论文,wordpress 浏览器上显示错位,广州网站百度排名推广,dede能建立手机网站吗先看代码#xff0c;定义一个简单的泛型 c1 里面一个 int #xff0c;定义一个函数goods 下面 main函数进行调用, 如果直接传int 类型是不会报错的,但是如果传自定义类型的b就会报错。
type c1 interface {int
}func goods[T c1](a T) {fmt.Println(a)
}type myint intfunc …先看代码定义一个简单的泛型 c1 里面一个 int 定义一个函数goods 下面 main函数进行调用, 如果直接传int 类型是不会报错的,但是如果传自定义类型的b就会报错。
type c1 interface {int
}func goods[T c1](a T) {fmt.Println(a)
}type myint intfunc main() {var a int 1var b myint 2goods(a)goods(b) error:myint does not satisfy c1 (possibly missing ~ for int in c1)compilerInvalidTypeArg
}对面代码改成就不会有问题
type c1 interface {~int
}解释 “~” 符号的类型则代表以该类型为底层类型underlying type的所有类型这类带有 “~” 的类型也被称为 approximation elemen