Challenge: Write Code with a Pipeline Pattern
Learn to use the pipeline pattern and see its practical implementation.
Problem statement
Let’s work on an easy challenge that consists of these four steps:
- Read the data from the file and store it in an array of strings. Follow the steps below:
-
Open the file and read the data. If the file is unreadable, it will throw an error.
-
Create the output channel.
-
Import the
encoding/csv
packageencoding/csv
to perform the operations on the CSV file. -
Initialize the reading of the file line by the line inside a goroutine. Use
NewReader
andFieldsPerRecord
inside the goroutine to read the CSV file.
-
2.Swap the values in the following format. Use input from the previous steps and pass the channel to the next step.
c[0], c[1], c[2] = c[1], c[2], c[0]
- Concatenate
9
at the start and0
at the end of each array value.
-
Display the array:
fmt.Sprintf("First: %s, Second: %s, Third: %s", c[0], c[1], c[2])
Note: We can also write everything in the same file, but writing each function in a new file makes it easy to use and understand.
Code
Write your solution in the comments below:
Get hands-on with 1300+ tech skills courses.