Define a printLines function that takes a slice of strings as a parameter, and prints each element of that slice on a separate line.
Then, in main, get a slice of the daysOfWeek array containing just the weekdays: “Monday”, “Tuesday”, “Wednesday”, “Thursday”, and “Friday”. Pass that slice to printLines.
The program below should ask the user to input several strings. When the user is done, it should output the strings the user entered, separated by commas. Comments within the code explain how it all works. The finished program will rely on the strings.Join function to join the strings together. You can read documentation on strings.Join here.
Your task is to update the code in the main function to get it all working. Set up a variable with a slice of strings to hold the phrases the user enters. Add new phrases to the end of the slice. And call strings.Join to join the strings in the finished slice with commas.
Sample output:
String to add: cashews
Continue? [Y/n]: y
String to add: peanuts
Continue? [Y/n]: y
String to add: almonds
Continue? [Y/n]: n
cashews, peanuts, almonds
CSV is a common file format used to represent spreadsheet data in plain-text files. Go includes an encoding/csv package that can read this format.
We’re working on a program that accepts the name of a CSV file and a column number as command-line arguments. The program should go through the file and print only the requested column.
So, for example, if you save the following data as gophers.csv:
And you run the program with pcolumn gophers.csv 2, it should output:
last_name
Pike
Thompson
Griesemer
We’ve written the printColumn function for you, which reads the specified file, prints the specified column, and returns a non-nil error value if it encounters any problems. We’ve also written a check function that will log an error an exit the program, unless the error is nil.
Your task is to update the main function to read the two command-line arguments, check that they’re valid, and use them to make an appropriate call to printColumn. We’ve added comments to main that describe what you’ll need to do. You’ll need to call strconv.ParseInt to parse the column number argument, so you might need to refer to the strconv.ParseInt documentation briefly.