Search code examples
oopgostructembeddingcomposite-literals

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


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.


Solution

  • 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.