title: List of elements of different types (when trying to build a parser)
body: <p>I am trying to write a simple parser in Go (I am a beginner in Go). I already wrote multiple parsers, but using object oriented langages (Java or C++). By the way, I am also familiar with C programming, so I am used to procedural langage. But here, I am in trouble with a very simple problem.</p>
  <p>I'd like to parse something and return a list of Statement. But &quot;Statement&quot; is an abstract object. In Java, I would have done it this way :</p>
  <pre><code>public class main {
      abstract class Statement {...}
      class Node1 extends Statement {...}
      class Node2 extends Statement {...}
  
      List&lt;Statement&gt; parse(...) {....}
  
      public static void main(String[] args) {
          List&lt;Statement&gt; stmts = parse(...);
          for (final Statement stmt : stmts) {
              stmts.generate()
          }
      }
  }
  </code></pre>
  <p>To do more or less the same, I wrote this in Go :</p>
  <pre><code>type Statement interface {
      Generate()
  }
  
  type Node1 struct {}
  func (w Node1) Generate() {}
  
  type Node2 struct {}
  func (w Node2) Generate() {}
  
  func parse(...) ([]Statement, error) {...}
  
  func main() {
      stmts, e := parse(...)
      for _, stmt := range stmts {
          stmt.Generate()
      }
  }
  </code></pre>
  <p>This seems to work, but I am sure that there is a better way to do it. In C, there are no such thing as <code>interface</code> and yet a lot of parser are written in C.</p>
  <p>Anybody have an idea to solve my problem using either a more C-style solution or a more Go-style solution ?</p>