将其他pkg的类型嵌入到我的中,并通过字面量进行初始化

Embed an type of other pkg into mine, and init it by literal

我阅读了如何初始化 embed type, and a related

我在编译这段代码时遇到的问题是:

[错误] dala02 类型的结构文字中的未知字段 'feature.DefaultSshHelper'

type FDH feature.DefaultSshHelper                                                                                                                                                                            

type dala02 struct {                                                                                                                                                                                         
    Md5_v string                                                                                                                                                                                             
    feature.DefaultSshHelper                                                                                                                                                                                 
     //FDH                                                                                                                                                                                                    
}                                                                                                                                                                                                            

var x_01_h1_p = &dala02{                                                                                                                                                                                     
    Md5_v: "",                                                                                                                                                                                               
    feature.DefaultSshHelper: feature.DefaultSshHelper{                                                                                                                                                      
        //FDH: FDH{  
            // blabla
    },
}
// use it by a interface []feature.CmdFioHelper{x_00_h1_p}        

一开始以为是Exported的问题,所以加了这一行'type FDH feature.DefaultSshHelper'。现在,我们有这个错误:

[错误] 不能在数组或切片文字中使用 x_01_h1_p(类型 *dala02)作为类型 feature.CmdFioHelper: *dala02 未实现 feature.CmdFioHelper(缺少 Getnextchecker 方法)

但是 feature.DefaultSshHelper 的指针确实实现了 feature.CmdFioHelper(接口)。所以 dala02 的指针也应该实现它,对吧? (参考表格有效)

There's an important way in which embedding differs from subclassing. When we embed a type, the methods of that type become methods of the outer type, but when they are invoked the receiver of the method is the inner type, not the outer one.

问题是如何修复这个编译错误,哪一行错了?我不是 golang 专家,感谢您的建议 :)。顺便说一句,我确实找到了一些解决方法。

当你引用嵌入字段时,你必须省略嵌入类型的包名,因为非限定类型名充当字段名。

Spec: Struct types:

A field declared with a type but no explicit field name is an anonymous field, also called an embedded field or an embedding of the type in the struct. An embedded type must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type. The unqualified type name acts as the field name.

所以简单写:

var x_01_h1_p = &dala02{
    Md5_v:            "",
    DefaultSshHelper: feature.DefaultSshHelper{
    // blabla
    },
}

您的其他尝试 type FDH feature.DefaultSshHelper 失败了,因为此类型声明创建了一个具有零方法的新类型:类型 FDH 没有 "inherit" feature.DefaultSshHelper 的方法。因此任何嵌入 FDH 的类型也将缺少 feature.DefaultSshHelper.

的方法