在 go 中,若需将工厂函数注册为接口实现的回调,必须让函数返回接口类型(如 texture),而非具体结构体指针(如 *ddstexture),否则因类型身份不匹配导致编译失败。
Go 的类型系统严格区分底层类型(underlying type) 和接口实现关系。虽然 *DDSTexture 实现了 resource.Texture 接口,但函数签名中的返回类型 *DDSTexture 与 func(string) *resource.Texture 中的 *resource.Texture 并不等价——因为 *resource.Texture 是一个指向接口值的指针(极少使用且通常错误),而 *DDSTexture 是指向具体类型的指针。更重要的是,Go 不允许将返回具体类型指针的函数直接赋值给返回接口类型(或其指针)的函数类型,哪怕该具体类型实现了对应接口。
✅ 正确做法是:*让工厂函数返回接口类型本身(即 Texture),而非 `DDSTexture`**。这样既符合类型约束,又充分利用了 Go 接口的动态多态特性:
// texture/dds.go
package texture
import "your-module/resource"
type DDSTexture struct {
path string
_tid uint32
height uint32
width uint32
}
func NewDDSTexture(filename string) resource.Texture { // ✅ 返回 interface{} 类型
return &DDSTexture{
path: filename,
_tid: 0,
height: 0,
width: 0,
}
}
func (d *DDSTexture) Texture() (uint32, error) { /* 实现... */ }
func (d *DDSTexture) Width() int { return int(d.width) }
func (d *DDSTe
xture) Height() int { return int(d.height) }
func init() {
resource.AddTextureLoader("dds", NewDDSTexture) // ✅ 类型匹配:func(string) resource.Texture
}同时,更新 resource/resource.go 中的注册函数签名,明确接受返回接口的函数:
// resource/resource.go
package resource
var (
tex_types map[string]func(string) Texture = make(map[string]func(string) Texture)
)
type Texture interface {
Texture() (uint32, error)
Width() int
Height() int
}
func AddTextureLoader(ext string, fn func(string) Texture) {
tex_types[ext] = fn
}⚠️ 注意事项:
总结:Go 的函数类型要求精确的类型匹配,接口实现 ≠ 类型兼容。解决此类回调注册问题的核心原则是——让工厂函数的返回类型直接声明为接口类型,由运行时自动装箱具体实现。这既是类型安全的体现,也是 Go 面向接口编程的最佳实践。