Skip to content
gqlxj1987's Blog
Go back

Filtering Logic in Go

Edit page

原文链接

The simplest solution may be to do filtering in-line, looping through your set of records and discarding whatever records don’t match your business requirements

Choosing a filtering library comes at cost though — libraries have to be type-agnostic and thus will generally be using reflect under the hood to do type assertions which is not ideal for performance. 关于filtering库的一个实现思路

// Filter is a filter function applied to a single record.
type Filter func(string) bool

// FilterBulk is a bulk filter function applied to an entire slice of records.
type FilterBulk func([]string) []string
// ApplyFilters applies a set of filters to a record list.
// Each record will be checked against each filter.
// The filters are applied in the order they are passed in.
func ApplyFilters(records []string, filters ...Filter) []string {
    // Make sure there are actually filters to be applied.
    if len(filters) == 0 {
        return records
    }

    filteredRecords := make([]string, 0, len(records))

    // Range over the records and apply all the filters to each record.
    // If the record passes all the filters, add it to the final slice.
    for _, r := range records {
        keep := true

    for _, f := range filters {
            if !f(r) {
                keep = false
                break
            }
        }

    if keep {
            filteredRecords = append(filteredRecords, r)
        }
    }

    return filteredRecords
}

// ApplyBulkFilters applies a set of filters to the entire slice of records.
// Used when each record filter requires knowledge of the other records, e.g. de-duping.
func ApplyBulkFilters(records []string, filters ...FilterBulk) []string {
    for _, f := range filters {
        records = f(records)
    }

    return records
}




var filters = map[int]FilterSet{ 
    1: FilterForAnimals, 
    2: FilterForIDs, 
}


// FilterForAnimals applies a set of filters removing any non-animals.
func FilterForAnimals(records []string) []string {
    return ApplyBulkFilters(
        ApplyFilters(records,
            FilterMagicalCreatures,
            FilterStringLength,
            FilterInts,
            FilterWords,
        ),
        FilterDuplicates,
    )
}

// FilterForIDs applies a set of filters removing any non-IDs.
func FilterForIDs(records []string) []string {
    return ApplyBulkFilters(
        ApplyFilters(records,
            FilterIDs,
        ),
        FilterDuplicates,
    )
}

Edit page
Share this post on:

Previous Post
Rest vs GRPC
Next Post
克服懒