func writeValuesTofile(datatowrite *ProtobufTest.TestMessage) { //Retreive client information from the protobuf message ClientName := datatowrite.GetClientName() ClientDescription := datatowrite.GetDescription() ClientID := strconv.Itoa(int(datatowrite.GetClientId())) // retrieve the message items list items := datatowrite.GetMessageitems() fmt.Println("Writing value to CSV file") //Open file for writes, if the file does not exist then create it file, err := os.OpenFile("CSVValues.csv", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666) checkError(err) //make sure the file gets closed once the function exists defer file.Close() //Go through the list of message items, insert them into a string array then write them to the CSV file. writer := csv.NewWriter(file) for _, item := range items { record := []string{ClientID, ClientName, ClientDescription, strconv.Itoa(int(item.GetId())), item.GetItemName(), strconv.Itoa(int(item.GetItemValue())), strconv.Itoa(int(item.GetItemType()))} writer.Write(record) fmt.Println(record) } //flush data to the CSV file writer.Flush() fmt.Println("Finished Writing value to CSV file") }