title: Marshal Go Struct to BSON for mongoimport
body: <p>I have a slice of structs that I want to write to a BSON file for doing a <code>mongoimport</code>.</p>
  <p>This a rough idea of what I'm doing (using <code>gopkg.in/mgo.v2/bson</code>):</p>
  <pre><code>type Item struct {
      ID   string `bson:&quot;_id&quot;`
      Text string `bson:&quot;text&quot;`
  }
  
  items := []Item{
      {
          ID: &quot;abc&quot;,
          Text: &quot;def&quot;,
      },
      {
          ID: &quot;uvw&quot;,
          Text: &quot;xyz&quot;,
      },
  }
  
  file, err := bson.Marshal(items)
  if err != nil {
      fmt.Printf(&quot;Failed to marshal BSON file: '%s'&quot;, err.Error())
  }
  
  if err := ioutil.WriteFile(&quot;test.bson&quot;, file, 0644); err != nil {
      fmt.Printf(&quot;Failed to write BSON file: '%s'&quot;, err.Error())
  }
  </code></pre>
  <p>This runs and generates the file, but it's not formatted correctly - instead it looks like this (using <code>bsondump --pretty test.bson</code>):</p>
  <pre><code>{
      &quot;1&quot;: {
          &quot;_id&quot;: &quot;abc&quot;,
          &quot;text&quot;: &quot;def&quot;
      },
      &quot;2&quot;: {
          &quot;_id&quot;: &quot;abc&quot;,
          &quot;text&quot;: &quot;def&quot;
      }
  }
  </code></pre>
  <p>When I think it should look more like:</p>
  <pre><code>{
      &quot;_id&quot;: &quot;abc&quot;,
      &quot;text&quot;: &quot;def&quot;
  {
  }
      &quot;_id&quot;: &quot;abc&quot;,
      &quot;text&quot;: &quot;def&quot;
  }
  </code></pre>
  <p>Is this possible to do in Go? I just want to generate a <code>.bson</code> file that you would expect a <code>mongodump</code> command to produce, so that I can run <code>mongoimport</code> and populate a colection.</p>