Write to csv in GO error

csv, go

Solution

There is an additional flag, as you are calling os.OpenFile directly instead of via os.Create which passes O_RDWR for you. you need to either pass O_RDWR or O_WRONLY along with your O_APPEND | O_CREATE flags.

O_APPEND is only used to say that writes should be appended to the end of the file when writing, it doesn't specify the open mode itself.

Additional Note: you're using "error" as a variable name, this is actually a type in Go, the idiom generally used for naming the error return variable is "err"

Problem

I am trying to write to a csv file in GO using the standard library "encoding/csv" that comes with GO. However, nothing is being written to the file and there are no errors returning either. Below is the writing code. The values in the map are string arrays. Thanks for any help in advance. ``` func writeErrors() { file, error := os.OpenFile("output.csv", os.O_APPEND|os.O_CREATE, 0666 ) if error != nil {panic(error)} defer file.Close() // New Csv writer writer := csv.NewWriter(file) // Headers var new_headers = []string { "group_id", "account_id", "location_id", "payment_rating", "records_with_error" } returnError := writer.Write(new_headers) if returnError != nil { fmt.Println(returnError) } for key, value := range errors { returnError := writer.Write(value) if returnError != nil { fmt.Println(returnError) } fmt.Println("Writing: ", key, value) } } ```

Original source