Appending songs to a CSV file

Report a typo

Another common file format apart from .txt files are the .csv files — CSV stands for Comma Separated Values.

A CSV file stores values that are separated by a specific delimiter, commonly a comma (,). However, there are other common delimiters like the semicolon (;), a tab (\t), blank spaces (" ") and even the pipe character (|).

Now that you've been acquainted with CSV files, your task is to create a Go program that appends the comma-separated values contained within the data variable into the songs.csv file.

Below you can see the current contents of the songs.csv file:

Title,Artist,Album,Duration
"Fix You","Coldplay","X&Y",4:56
"Clocks","Coldplay","A Rush of Blood to the Head",5:08
"Yellow","Coldplay","Parachutes",4:27
"Summertime Sadness","Lana Del Rey","Born to Die",4:25
"Young and Beautiful","Lana Del Rey","Born to Die",3:56
"Pumped Up Kicks","Foster the People","Torches",3:59

To do this, you need to use the os.OpenFile() function first to open songs.csv with the os.O_APPEND, os.O_WRONLY flags and with permission mode 0644.

Then you can use the fmt.Fprintln() function to append the comma-separated values of songs contained within the data variable to the songs.csv file.

Write a program
package main

import (
"fmt"
"log"
"os"
)

// nolint: gomnd // <-- DO NOT delete this line!
func main() {
// DO NOT modify the contents of the `data` variable!
data := `"One","Metallica","...And Justice for All",7:27
"Fuel","Metallica","Reload",4:30
"Master of Puppets","Metallica","Master of Puppets",8:36
`
// Write the code to append the additional records to the "songs.csv" file below
// Use the os.OpenFile() function first to open "songs.csv" with the os.O_APPEND and os.O_WRONLY flags
// And then you can use the fmt.Fprintln() function to write the data to the file.
}

___

Create a free account to access the full topic