Mastering Data Aggregation and Data Streams with Go: Building a Sales Records Aggregator

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:

package main

import (
    "fmt"
    "time"
)

type sale struct {
    amount float64
    date   time.Time
}

type SalesAggregator struct {
    sales map[string]sale
}

func (s *SalesAggregator) AddSale(saleID string, amount float64, date time.Time) {
    s.sales[saleID] = sale{amount: amount, date: date}
}

func (s *SalesAggregator) GetSale(saleID string) (float64, bool) {
    sale, exists := s.sales[saleID]
    return sale.amount, exists
}

func (s *SalesAggregator) DeleteSale(saleID string) bool {
    if _, exists := s.sales[saleID]; exists {
        delete(s.sales, saleID)
        return true
    }
    return false
}

func main() {
    aggregator := SalesAggregator{sales: make(map[string]sale)}

    // Add sales
    aggregator.AddSale("001", 100.50, time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC))
    aggregator.AddSale("002", 200.75, time.Date(2023, 1, 15, 0, 0, 0, 0, time.UTC))

    // Get sale
    if amount, found := aggregator.GetSale("001"); found {
        fmt.Println(amount) // Output: 100.5
    } else {
        fmt.Println("Sale not found")
    }

    // Delete sale
    fmt.Println(aggregator.DeleteSale("002")) // Output: true
    if amount, found := aggregator.GetSale("002"); found {
        fmt.Println(amount)
    } else {
        fmt.Println("Sale not found") // Output: Sale not found
    }
}

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.

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