title: Set fields using Reflection
body: <p>I am struggling with some Go lang code for a few days.　I have a golang function that would set all the common fields like createdBy, updatedBy, etc inside a struct. I have googled a lot and have come up with the following code.</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "reflect"
      "time"
      "strings"
  )
  type User struct {
      UserId         string         `json:"userId"`
      ObjectType         string         `json:"objectType"`
      CreationDate       string         `json:"creationDate"`
      UpdationDate       string         `json:"updationDate"`
      Version            int            `json:"version"`         
  }
  func main() {
      //fmt.Println("Hello, playground")
      var user = User{}
      var k = setCommonParam(&amp;user )
      var p = k.(*User)
      fmt.Println(p.CreationDate)
      var l = *p
      fmt.Println(l.ObjectType)
      fmt.Println(reflect.TypeOf(k))
      fmt.Println(reflect.TypeOf(user))
  
  }
  
  func setCommonParam(obj interface{}) (interface{}) {
      entityValue := reflect.ValueOf(obj).Elem()
      entityType := entityValue.Type()
  
      for i:=0; i&lt;entityValue.NumField(); i++ {
          typeField := entityType.Field(i)
          vField := entityValue.Field(i)
  
          if typeField.Name == "ObjectType" {
              vField.SetString(strings.ToLower(reflect.TypeOf(obj).Elem().Name()))
          } else if typeField.Name == "CreationDate" {    
              vField.SetString(time.Now().Format(time.RFC3339))   
          } else if typeField.Name == "UpdationDate" {
              vField.SetString(time.Now().Format(time.RFC3339))
          } else if typeField.Name == "CreatedBy" {
  
          } else if typeField.Name == "UpdatedBy" {
  
          } else if typeField.Name == "Version" {
  
          }
      }
      return obj
  }
  </code></pre>
  
  <p>Is there any way I can write the following in a single line? </p>
  
  <pre><code>var k = setCommonParam(&amp;user )
  var p = k.(*User)
  var l = *p
  </code></pre>
  
  <p>and do something like this:</p>
  
  <pre><code>user = setCommonParam(user)
  </code></pre>
  
  <p>I am sorry but I am new to this. Thanks.</p>