This question already has an answer here:
I'm attempting to generate a CSV file which would store a dump of a MySQL query, using Go.
I am currently able to export my results to a pre-existing CSV file, but I am trying to auto generate the CSV file once main.go is run. I've tried to use WriteFile
, which I know will write a CSV file to the file name specified. I know this is by design, but I'd like the file to generate.
rows, _ := db.Query("SELECT * FROM orderTest limit 100;")
err := sqltocsv.WriteFile("orderTest.csv", rows)
if err != nil {
panic(err)
}
columns, _ := rows.Columns()
count := len(columns)
values := make([]interface{}, count)
valuePtrs := make([]interface{}, count)
for rows.Next() {
for i := range columns {
valuePtrs[i] = &values[i]
}
rows.Scan(valuePtrs...)
for i, col := range columns {
val := values[i]
b, ok := val.([]byte)
var v interface{}
if ok {
v = string(b)
} else {
v = val
}
fmt.Println(col, v)
}
}
}
My goal is to have the OrdeTest.csv file to auto create itself when I run main.go
</div>