title: How to pass data struct as parameter in golang
body: <p>XML to Json</p>
  
  <pre><code>package main
  
  import (
      "encoding/json"
      "encoding/xml"
      "fmt"
  )
  
  type Persons struct {
      Person []struct {
          Name string
          Age  int
      }
  }
  type Places struct {
      Place []struct {
          Name    string
          Country string
      }
  }
  
  type Parks struct {
      Park struct {
          Name     []string
          Capacity []int
      }
  }
  
  const personXml = `
      &lt;Persons&gt;
          &lt;Person&gt;&lt;Name&gt;Koti&lt;/Name&gt;&lt;Age&gt;30&lt;/Age&gt;&lt;/Person&gt;
          &lt;Person&gt;&lt;Name&gt;Kanna&lt;/Name&gt;&lt;Age&gt;29&lt;/Age&gt;&lt;/Person&gt;
      &lt;/Persons&gt;
  `
  
  const placeXml = `
      &lt;Places&gt;
          &lt;Place&gt;&lt;Name&gt;Chennai&lt;/Name&gt;&lt;Country&gt;India&lt;/Country&gt;&lt;/Place&gt;
          &lt;Place&gt;&lt;Name&gt;London&lt;/Name&gt;&lt;Country&gt;UK&lt;/Country&gt;&lt;/Place&gt;
      &lt;/Places&gt;
  `
  
  const parkXml = `
      &lt;Parks&gt;
          &lt;Park&gt;&lt;Name&gt;National Park&lt;/Name&gt;&lt;Capacity&gt;10000&lt;/Capacity&gt;&lt;/Park&gt;
          &lt;Park&gt;Asian Park&lt;/Name&gt;&lt;Capacity&gt;20000&lt;/Capacity&gt;&lt;/Park&gt;
      &lt;/Parks&gt;
  `
  
  func WhatIamUsing() {
      var persons Persons
      xml.Unmarshal([]byte(personXml), &amp;persons)
      per, _ := json.Marshal(persons)
      fmt.Printf("%s\n", per)
  
      var places Places
      xml.Unmarshal([]byte(placeXml), &amp;places)
      pla, _ := json.Marshal(places)
      fmt.Printf("%s\n", pla)
  
      var parks Parks
      xml.Unmarshal([]byte(parkXml), &amp;parks)
      par, _ := json.Marshal(parks)
      fmt.Printf("%s\n", par)
  }
  </code></pre>
  
  <p>What i want is a generic function which takes xml string and dataStruct
  and returns a Json output. But below function is throwing an error
  How to impliment this?</p>
  
  <pre><code>func Xml2Json(xmlString string, DataStruct interface{}) (jsobj string, err error) {
      var dataStruct DataStruct
      xml.Unmarshal([]byte(personXml), &amp;dataStruct)
      js, _ := json.Marshal(dataStruct)
      return fmt.Sprintf("%s\n", js), nil
  }
  
  func main() {
      jsonstring, _ := Xml2Json(personXml, Persons)
  }
  </code></pre>
  
  <p>Error message:</p>
  
  <p>prog.go:73: DataStruct is not a type</p>
  
  <p>prog.go:80: type Persons is not an expression</p>
  
  <p>goplay link: <a href="http://play.golang.org/p/vayb0bawKx">http://play.golang.org/p/vayb0bawKx</a></p>