Introduction

Welcome to our lesson on mastering data aggregation and data streams with Go. In this lesson, you'll learn to build a basic sales records aggregator using Go's standard library and map data structures. We'll extend this functionality to handle more advanced operations such as filtering, data aggregation, and formatting. By the end of this lesson, you'll be proficient in managing and formatting data streams efficiently in Go.

Starter Task Methods and Their Definitions

To get started, we'll create a simple sales record aggregator in Go. Here are the functions we'll focus on:

  • func (s *SalesAggregator) AddSale(saleID string, amount float64, date time.Time) - Adds or updates a sale record with a unique identifier saleID, amount, and date.

  • func (s *SalesAggregator) GetSale(saleID string) (float64, bool) - Retrieves the sale amount associated with the saleID. Returns the amount and a bool indicating if the sale exists.

  • func (s *SalesAggregator) DeleteSale(saleID string) bool - Deletes the sale record with the given saleID. Returns true if the sale was deleted, false if it does not exist.

Are these functions clear so far? Great! Let's now look at how we would implement them.

Starter Task Implementation

Here is the complete code for the starter task:

Let's quickly discuss this starter setup:

  • The sales map stores sale records with saleID as the key and a sale struct containing amount and date as the value.
  • AddSale adds a new sale or updates an existing sale ID.
  • GetSale retrieves the amount for a given sale ID and returns a bool to indicate if the sale exists.
  • DeleteSale removes the sale record for the given sale ID and returns a bool to indicate success.

Now that we have our basic aggregator, let's extend it to include more advanced functionalities.

New Methods and Their Definitions

To add complexity and usefulness to our sales aggregator, we'll introduce some additional functions for advanced data aggregation, filtering, and formatting functionalities.

  • func (s *SalesAggregator) AggregateSales(minAmount float64) (int, float64) - Returns the total number of sales and the total sales amount where the sale amount is above minAmount.

  • func (s *SalesAggregator) FormatSales(minAmount float64) string - Returns the sales data, filtered by minAmount, formatted as a plain text string. Includes sales statistics in the output.

  • func (s *SalesAggregator) GetSalesInDateRange(startDate, endDate time.Time) []SaleDetail - Retrieves all sales that occurred within the given date range, inclusive. Each sale includes saleID, amount, and date.

Let's implement these functions step by step.

Step 1: Implementing the 'Aggregate Sales' Method

We start by creating the AggregateSales function:

This method simply iterates through the sales and sums those that exceed the minAmount.

Step 2: Implementing the 'Format Sales' Method

Next, we create the FormatSales function to output data in plain text format.

Let's analyze this code:

  • The FormatSales method constructs a formatted string of sales data, filtered by a specified minimum amount.
  • It calculates total sales and amount for sales above minAmount using AggregateSales.
  • A strings.Builder is used to build the output string efficiently.
  • The WriteString method is called on the builder to append sections of the string, including sales details and summary.
  • The method filters sales, writing sale ID, amount, and date formatted as "YYYY-MM-DD" to the builder for qualifying sales.
  • Finally, a summary line is appended, and the constructed string is returned.
Step 3: Implementing the 'Get Sales in Date Range' Method

Now, let's finally implement GetSalesInDateRange, which relies on Go's time package to filter sales records.

In this code:

  • The GetSalesInDateRange method filters sales records within a specified date range using the time package.
  • It accepts startDate and endDate, both of type time.Time, which define the inclusive date range.
  • An empty slice of SaleDetail structs is initialized to store matching sales records.
  • The method iterates over the sales map and uses the time.Time methods Before and After to check if each sale's date falls within the range.
  • Sale records within the range are appended to the result slice as SaleDetail structs, which is ultimately returned.
Lesson Summary

Congratulations! You've extended a basic sales aggregator in Go to an advanced aggregator capable of filtering, aggregating, and formatting data using custom structs and Go's built-in features. These skills are pivotal in efficiently managing data streams, especially with large datasets in Go applications. Feel free to experiment with similar challenges to deepen your understanding. Well done!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal