Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To expand on Volker's answer a bit, you should be able to call </p> <pre><code>func BPlease() B { a_obj := A.APlease() // initialize the fields of A like normal b_obj := B{} // create a B, whose anonymous fields are not initialized yet b_obj.A = a_obj // PERHAPS WHAT YOU WANT: copy all a's fields to b's fields. // if you are relying sending a_obj's address somewhere in // APlease(), you may be out of luck. b_obj.field_unique_to_B = "initialized" return b_obj } </code></pre> <p>Now that you can create B objects with fields initialized by APlease(), you can call A's methods on B's objects, and even call A's methods from within B like so:</p> <pre><code>func (B_obj *B) HelloB(){ // can't call B_obj.HelloA() like you would expect B_obj.A.HelloA() // this works. Go "promotes" the anonymous field's methods // and fields to B_obj // but they don't appear in B_obj, they appear in B_obj.A fmt.Printf("And hello from B too; %s", B_obj.field_unique_to_B) } </code></pre> <p>I will echo Rick-777 here and suggest you stick to go's naming conventions and idioms; NewReader is much easier to read and understand than ReaderPlease.</p> <p>I contrived an example that I can put on bitbucket if people want. I think it's much easier to read when you are working with real metaphors; also a disclaimer - this is not the best code, but it does some things that answer your question.</p> <p>file: car/car.go</p> <pre><code>package car import "fmt" type BaseCar struct { Doors int // by default, 4 doors. SportsCar will have 2 doors Wheels int } func NewBaseCar() BaseCar { return BaseCar{Wheels: 4, Doors: 4} } // this will be used later to show that a "subclass" can call methods from self.BaseCar func (c *BaseCar) String() string { return fmt.Sprintf("BaseCar: %d doors, %d wheels", c.Doors, c.Wheels) } // this will be promoted and not redefined func (c *BaseCar) CountDoors() int { return c.Doors } </code></pre> <p>file sportscar/sportscar.go</p> <pre><code>package sportscar // You can think of SportsCar as a subclass of BaseCar. But go does // not have conventional inheritence, and you can paint yourself into // a corner if you try to force square c++ structures into round go holes. import ( "../car" ; "fmt" ) type SportsCar struct { car.BaseCar // here is the anonymous field isTopDown bool } func NewSportsCar() SportsCar { conv := SportsCar{} // conv.Wheels == 0 conv.BaseCar = car.NewBaseCar() // now conv.Wheels == conv.Doors == 4 conv.isTopDown = false // SportsCar-only field conv.Doors = 2 // Fewer Doors than BaseCar return conv } // SportsCar only method func (s *SportsCar) ToggleTop() { s.isTopDown = !s.isTopDown } // "overloaded" string method note that to access the "base" String() method, // you need to do so through the anonymous field: s.BaseCar.String() func (s *SportsCar) String() string { return fmt.Sprintf("Sports%s, topdown: %t", s.BaseCar.String(), s.isTopDown) } </code></pre> <p>file main.go</p> <pre><code>package main import ( "./car" ; "./sportscar" ; "fmt") type Stringer interface { // added this complication to show String() string // that each car calls its own String() method } func main() { boring := car.NewBaseCar() fancy := sportscar.NewSportsCar() fmt.Printf(" %s\n", Stringer(&amp;boring)) fmt.Printf("%s\n", Stringer(&amp;fancy)) fancy.ToggleTop() fmt.Printf("%s\n", Stringer(&amp;fancy)) fmt.Println("BaseCar.CountDoors() method is callable from a SportsCar:", fancy.CountDoors()) } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload