Challenge: Write Code with a Pipeline Pattern

Learn to use the pipeline pattern and see its practical implementation.

We'll cover the following

Problem statement

Let’s work on an easy challenge that consists of these four steps:

  1. Read the data from the file and store it in an array of strings. Follow the steps below:
    1. Open the file and read the data. If the file is unreadable, it will throw an error.

    2. Create the output channel.

    3. Import the encoding/csv package encoding/csv to perform the operations on the CSV file.

    4. Initialize the reading of the file line by the line inside a goroutine. Use NewReader and FieldsPerRecord 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]
  1. Concatenate 9 at the start and 0 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 1200+ tech skills courses.