Writing 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 (|).

Below you can see the contents of a sample CSV file:

Title,Artist,Album,Duration
"Anti-Hero","Taylor Swift","Midnights",3:21
"Midnight Rain","Taylor Swift","Midnights",2:54

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

To do this, you can use the os.WriteFile() function to write the data directly into songs.csv, or you can use the os.Create() function to create songs.csv first and then use fmt.Fprintln() to write the data into the file.

Write a program
package main

import (
"log"
"os"
)

func main() {
// DO NOT modify the contents of the `data` variable!
data := `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
`
// Write the data to the "songs.csv" file below
// You can use the os.WriteFile() to write the data directly to "songs.csv"
// Or you can use the os.Create() to create "songs.csv" and then use fmt.Fprintln() to write the data.
}

___

Create a free account to access the full topic