Introduction

Welcome to today's lesson on applying data filtering and aggregation in a real-world scenario using a user management system in Go. We'll start by building a foundational structure that can handle basic user operations. Then, we'll expand it by introducing more advanced functionalities that allow filtering and aggregating user data.

Starter Task Methods

In our starter task, we will implement a set of functions that manage basic operations on a collection of user data, specifically handling adding new users, retrieving user profiles, and updating user profiles.

Here are the starter task methods:

  • addUser(userID string, age int, country string, subscribed bool) bool - Adds a new user with the specified attributes. The parameters are passed by value since you're providing complete new values for the addition, and there's no need to track changes after the function call. Returns true if the user was added successfully and false if a user with the same userID already exists.
  • getUser(userID string) *UserProfile - Returns a pointer to the user's profile if the user exists; otherwise, returns nil.
  • updateUser(userID string, age *int, country *string, subscribed *bool) bool - Updates the user's profile based on non-nil parameters. Differently from addUser, notice the use of pointers which allows for selective updates; by passing a nil, you indicate that a specific field should remain unchanged. Returns true if the user exists and was updated; false otherwise.

To store the user data, we will define a UserProfile struct.

Starter Task Implementation

Here is the implementation of our starter task in Go:

The code provides a basic user management system in Go:

  • It uses the UserProfile struct to store user details such as age, country, and subscription status.
  • The UserManager struct manages user profiles in a map, with user IDs as keys.
  • The NewUserManager function initializes UserManager with an empty map of users.
  • Helper functions (intPointer, stringPointer, boolPointer) are included to allow optional parameters to be easily passed when updating user profiles.
  • In the main function, users are added, retrieved, and updated to demonstrate the functionality of the user management system.
Step 1: Adding 'filterUsers' Method

This method filters users based on the criteria provided. Let's see how it works:

In this code snippet:

  • The filterUsers method filters users based on minAge, maxAge, country, and subscribed status criteria.
  • It iterates over the users map and checks each user's profile against the provided criteria.
  • Users who meet all the criteria are added to the filteredUsers list, which is then returned.
  • The example usage shows how to add users and filter them based on different criteria.
Step 2: Adding 'aggregateStats' Method

This method aggregates statistics from the user profiles. Let's implement it:

Let's discuss this new functionality:

  • The aggregateStats method calculates and returns aggregate statistics about the users in the form of a map.
  • It determines totalUsers as the total number of users.
  • If there are no users, it returns a map with zeroed statistics.
  • It calculates totalAge by summing the ages of all users.
  • It computes subscribedCount by counting the users who are subscribed.
  • It computes averageAge by dividing totalAge by totalUsers.
  • It calculates subscribedRatio by dividing subscribedCount by totalUsers.
  • The resulting statistics map includes total_users, average_age, and subscribed_ratio.
Lesson Summary

Great job! Today, you've learned how to effectively handle user data in Go by implementing advanced functionalities like filtering and aggregation on top of a basic system. This is a critical skill in real-life software development, where you often need to extend existing systems to meet new requirements.

I encourage you to practice solving similar challenges to solidify your understanding of data filtering and aggregation in Go. Happy coding, and see you in the next lesson!

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