Golang中接口是否必须严格声明方法签名?
Golang中接口是否必须严格声明方法签名?
如果我想使用一个符合外部包(例如 *template.Template)签名定义的接口,我是否必须逐字写出它的所有签名?
type Tempi interface{
AddParseTree(name string, tree *parse.Tree) (*template.Template, error)
Clone() (*template.Template, error)
DefinedTemplates() string
Delims(left, right string) *template.Template
[...]
}
有没有办法通过单个语句来继承所有签名?
type Tempi interface{
*template.Template
}
Tempi 显然可以编译,但它只能用作 type constraint。我希望 Tempi 能继承 *template.Template 的所有签名,这可能吗?
更多关于Golang中接口是否必须严格声明方法签名?的实战教程也可以访问 https://www.itying.com/category-94-b0.html
3 回复
你好,如果 *template.Template 是一个结构体,那么据我所知,不行。你不能将它传递给接口。但如果它在外部包中有接口描述,那么你可以使用它,并根据需要添加任何附加功能,将其嵌入到你自定义的类型中。


