type QueryOption interface { ApplyTo(users []*User) []*User }
funcFindUser(users []*User, options ...QueryOption) []*User { for _, option := range options { users = option.ApplyTo(users) } return users }
type Where struct { Name string Age int }
func(w *Where) ApplyTo(users []*User) []*User { res := make([]*User, 0, len(users)) for _, u := range users { if u.Name == w.Name && u.Age == w.Age { res = append(res, u) } } return res }
type Range struct { Offset int Limit int }
func(r *Range) ApplyTo(users []*User) []*User { res := make([]*User, 0, len(users)) for i := r.Offset; i < len(users) && i < r.Offset+r.Limit; i++ { res = append(res, users[i]) } return res }