title: is it possible to call overridden method from parent struct in Golang?
body: <p>I want to implement such code, where B inherit from A and only override A's Foo() method, and I hope the code to print B.Foo(), but it still print A.Foo(), it seems that the receiver in Golang can't work like this in C++, in which when dynamic binding is enabled, the code can work like what I want. </p>
  
  <p>I also post another piece of code, which works, but it's too hard to implement, and more like a hack way, I think it's not a Golang style.</p>
  
  <p>So my problem is: if the parent's Bar() method has some logic, for example, open a file, then read some lines, and use Foo() to output these lines to <code>stdout</code>, and the Child (in the example is B) wants to use most of them, the only difference is that the Child wants Foo() to output the lines to another file. How should I implement it? I have heard that Golang's inheritance can't work like C++ or Java, and what's right way in Golang?</p>
  
  <pre><code>package main 
  
  import ( 
          "fmt" 
  ) 
  
  type A struct { 
  } 
  
  func (a *A) Foo() { 
          fmt.Println("A.Foo()") 
  } 
  
  func (a *A) Bar() { 
          a.Foo() 
  } 
  
  type B struct { 
          A 
  } 
  
  func (b *B) Foo() { 
          fmt.Println("B.Foo()") 
  } 
  
  func main() { 
          b := B{A: A{}} 
          b.Bar() 
  }
  
  output: A.Foo()
  </code></pre>
  
  <p>the following piece works, but when write </p>
  
  <pre><code>a := A{}
  a.Bar()
  </code></pre>
  
  <p>you will encounter a compiler error</p>
  
  <pre><code>package main
  
  import (
      "fmt"
  )
  
  type I interface {
      Foo()
  }
  
  type A struct {
      i I
  
  }
  
  func (a *A) Foo() {
      fmt.Println("A.Foo()")
  }
  
  func (a *A) Bar() {
      a.i.Foo()
  
  }
  
  type B struct {
      A
  }
  
  func (b *B) Foo() {
      fmt.Println("B.Foo()")
  }
  
  func main() {
      b := B{A: A{}}
      b.i = &amp;b     // here i works like an attribute of b
      b.Bar()
  
  output: B.Foo()
  </code></pre>