go filter

2026-08-30 浏览 (1)

filter.go 源码

// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// For more tutorials  : https://learngoprogramming.com
// In-person training  : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus

package pipe

// FilterFunc represents a filtering pipeline func.
// The type alias frees us from binding to a named type.
type FilterFunc = func(Record) (pass bool)

// Filter the records.
type Filter struct {
	src     Iterator
	filters []FilterFunc
}

// FilterBy returns a new filter pipeline.
func FilterBy(fn ...FilterFunc) *Filter {
	return &Filter{filters: fn}
}

// Consume the records for lazy filtering.
func (f *Filter) Consume(records Iterator) error {
	f.src = records
	return nil
}

// Each filtered records.
func (f *Filter) Each(yield func(Record) error) error {
	records := func(r Record) error {
		if !f.checkAll(r) {
			return nil
		}
		return yield(r)
	}

	return f.src.Each(records)
}

// checkAll the filters against the record.
func (f *Filter) checkAll(r Record) bool {
	for _, fi := range f.filters {
		if !fi(r) {
			return false
		}
	}
	return true
}

你可能感兴趣的文章

go record

go textreport

go filters

go pipe

go close

go jsonreport

go jsonlog

go group

go pipeline

go recordshare

  • 所属分类: 后端技术
  • 本文标签: golang
  • 版权声明: 本文链接 https://seaxiang.com/blog/s7HFk8yU