duanchuli5647 2017-12-13 07:18
浏览 17
已采纳

将其他类型的pkg嵌入我的,然后按字面量将其初始化

I read how to init embed type, and a related Q&A.

What my problem is when compile this code, I got :

[Error] unknown field 'feature.DefaultSshHelper' in struct literal of type dala02

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}        

At first time, I thought it was an Exported problem, so I added this line 'type FDH feature.DefaultSshHelper'. Now, we have this error :

[Error] cannot use x_01_h1_p (type *dala02) as type feature.CmdFioHelper in array or slice literal: *dala02 does not implement feature.CmdFioHelper (missing Getnextchecker method)

But a pointer of feature.DefaultSshHelper does implement feature.CmdFioHelper ( a interface ). So pointer of dala02 should also implement that, right? (reference form effective go)

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.

Question is how to fix this compile error, which line is wrong? I'm not a expert for golang, thanks for your advice :). BTW I do find some workaround.

  • 写回答

1条回答 默认 最新

  • duanne9313 2017-12-13 07:44
    关注

    When you refer to embedded fields, you have to leave out the package name of the embedded type, as the unqualified type name acts as the field name.

    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.

    So simply write:

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

    Your other attempt type FDH feature.DefaultSshHelper falls short as this type declaration creates a new type with zero methods: the type FDH does not "inherit" the methods of feature.DefaultSshHelper. And thus any type that embeds FDH will also lack methods of feature.DefaultSshHelper.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog