title: Writing a struct's fields and values of different types to a file in Go
body: <p>I'm writing a simple program that takes in input from a form, populates an instance of a struct with the received data and the writes this received data to a file. </p>
  
  <p>I'm a bit stuck at the moment with figuring out the best way to iterate over the populated struct and write its contents to the file.</p>
  
  <p>The struct in question contains 3 different types of fields (ints, strings, []strings).</p>
  
  <p>I can iterate over them but I am unable to get their actual type. </p>
  
  <p>Inspecting my posted code below with print statements reveals that each of their types is coming back as structs rather than the aforementioned string, int etc.</p>
  
  <p>The desired output format is be plain text.</p>
  
  <p>For example:</p>
  
  <pre><code>field_1="value_1"
  field_2=10
  field_3=["a", "b", "c"]
  </code></pre>
  
  <p>Anyone have any ideas? Perhaps I'm going about this the wrong way entirely?</p>
  
  <pre><code>func (c *Config) writeConfigToFile(file *os.File) {
  
      listVal := reflect.ValueOf(c)
      element := listVal.Elem()
  
      for i := 0; i &lt; element.NumField(); i++ {
          field := element.Field(i)
          myType := reflect.TypeOf(field)
  
          if myType.Kind() == reflect.Int {
              file.Write(field.Bytes())
              } else {
                  file.WriteString(field.String())
              }
          }
  }
  </code></pre>