我有一个我无法更改的功能,该功能看起来像foo(interface{}). 在其他一些类型中,此函数可以采用类型[]byte但不能采用[16]byte. 我想写一个基于泛型的小适配器来添加对 UUID 的支持而不是编写foo(uuid[:]). 但我不想挂在特定的实现上。例如,而不是
import (
gofrsuuid "github.com/gofrs/uuid"
googleuuid "github.com/google/uuid"
)
type AcceptedCertainTypes interface {
int | gofrsuuid.UUID | googleuuid.UUID // | etc...
}
我希望有
type AcceptedTypes interface {
int | ~[16]byte
}
但我不知道该怎么做。当我们使用某些类型时,很容易将它们变成正确的类型。
func rewrittenFoo[T AcceptedCertainTypes](val T) {
var t interface{} = *new(T)
switch t.(type) {
case gofrsuuid.UUID:
k := val.(gofrsuuid.UUID)
foo(k[:])
case googleuuid.UUID:
k := val.(googleuuid.UUID)
foo(k[:])
}
}
但是如何将interface{}contains转换gofrsuuid.UUID为该基本类型[16]byte?
MMMHUHU
相关分类