title: How do I write a function for multiple types in Golang?
body: <p>I'm trying to write a helper function that can take in different custom Types in Golang, but I can't figure out how to do it exactly the way I want. Here's the situation (incidentally, I'm building an API that returns JSON objects implementing HAL protocol. This just means that resources and relationships are returned as links and not simply ID'). </p>
  
  <p>I have a number of models in my app, such as Student, Principal, School, etc...
  Each of these models has many fields, some same, some different. Ideally, I'd like a function that can iterate through the fields of a struct, and change another field in the struct. The big challenge is that these structs can be of type Student, Principal, School, etc...</p>
  
  <p>Models:</p>
  
  <pre><code>type Person struct {
      halgo.Links
      Id        bson.ObjectId 
      Firstname string        
      Lastname  string        
      Email     string        
  }
  
  type Student struct {
      Person `bson:",inline"`
  
      School mgo.DBRef
  }
  
  type School struct {
      Id      bson.ObjectId
      Address []string     
      Name    string       
      Description string
  }
  </code></pre>
  
  <p>Then, I'd like a function that can basically take any of these structs, iterate through the fields (using <code>reflect</code>), and do something with each field.</p>
  
  <p>I've tried a function that takes <code>interface{}</code> but the problem is you have to type-assert the argument to have access to any of the fields. And even once you have that, you'll still have to write individual functions for each type/model anyways.:</p>
  
  <pre><code>func GenerateLinksHelper(m interface{}) {
  ...
  }
  </code></pre>
  
  <p>Ultimately, I'm trying to find a way to write a function that takes in a somewhat arbitrary struct and perform operations on fields that may or may not be there. </p>