如何根据日期过滤集合并将其分配给地图

I have a struct that represents a Purchase:

type Purchase struct {
  id int64
  UserId int64
  CreatedAt  time.Time
}

Now I have a collection of these purchases.

On the UI side I am doing this:

  1. Get the current date, and display the last 2 weeks dates.
  2. If a purchase falls within a date, display it.

So it would look like:

Sunday May 29th
- date/timestamp purchase id, amount, etc.
- date/timestamp purchase id, amount, etc.
Saturday May 28th
- date/timestamp purchase id, amount, etc.
Friday May 27th
..
..
(past 2 weeks).

So my algorithm would be:

  1. get the current date
  2. using the current date, loop through the previous 2 weeks and search my Products collection and if the CreatedAt is the given date insert it into a collection
  3. On the UI side, loop through this collection, where the key is the day

So the current date is:

t := time.Now()

How can I traverse the past 2 weeks and then insert it into a map.

Is there a better algorithm that you would suggest?

An approach may be to arrange the entries in time-series structures and use that to filter required entries within some interval. We may use a map like map[timestamp_bucket][]Purchase. time-stamp bucket may be calculated as (time_stamp_corresponding_to_purchase_date_in_second - (time_stamp_corresponding_to_purchase_date_in_second % 3600)). Basically, we arrange the purchases in hourly buckets. When we want to get last two weeks purchases, we just compute the time_bucket keys that fall in last two weeks and retrieve the purchases from the corresponding buckets in the map. We may keep the slice []Purchase sorted if required if we want to use arbitrary time intervals to filter the records.