Home > other >  Novice go consult a interface problem
Novice go consult a interface problem

Time:10-19

Hope to be able to achieve the following functions, try for a long time has been wrong, consult everybody a great god

There is now a interface: I
 type I interface {
DO ()
}

I have two subclasses D1 and D2, D1 and D2 can occupy a unique resource, so you can't copy

There is now a struct: S, I hope to be able to run time assignment ii and DO function called
I think you can write
 
Type S struct {
Ii * I
}
//
Func (s * s) Do () {
S.i i.D O ()
}
//
D1: d1={}
D2: d2={}
S1: S={ii: d1}
S2: S={ii: d2}

But found that cannot compile
If written
 type S struct {
Ii I
}

There will be a copy here,,,
Tried a variety of position has always been not line,,,

CodePudding user response:

Step1:
Type I interface {
DO ()
}

Step2:
Type S struct {
Ii I
}

Step3:
D1:=& amp; D1 {}
D2:=& amp; D2 {}

Step4:
S1: S={ii: d1}
S2: S={ii: d2}

CodePudding user response:

DO inside the Interface is capitalized,
Func (s * s) DO () {//XXX}

CodePudding user response:

S1: S={ii: & amp; D1}
S2: S={ii: & amp; D2}

CodePudding user response:

I think when compiling problem has been submitted to the clear
S1: S={ii: & amp; D1}
S2: S={ii: & amp; D2}
To use the pointer address
  • Related