基于员工薪资的Go语言筛选程序
在使用Go语言处理数据集时,了解过滤器非常重要,因为在某些情况下,您可能需要分析数据以获得自定义结果。在本文中,我们将使用传统的循环方法、函数方法以及使用goroutine的方法来创建基于员工薪资的员工列表过滤器。
示例 1
在下面的代码中,“FilterEmployeeBySallary()”根据薪资范围筛选员工列表,并返回落在该薪资范围内的员工列表。
package main import "fmt" type Employee struct { Name string Salary int } func FilterEmployeesBySalary(employees []Employee, minSalary, maxSalary int) []Employee { var filteredEmployees []Employee for _, employee := range employees { if employee.Salary >= minSalary && employee.Salary <= maxSalary { filteredEmployees = append(filteredEmployees, employee) } } return filteredEmployees } func main() { employees := []Employee{ {Name: "Aman", Salary: 50000}, {Name: "Nitin", Salary: 70000}, {Name: "Akhi", Salary: 40000}, {Name: "Akshay", Salary: 60000}, } filtered := FilterEmployeesBySalary(employees, 45000, 65000) fmt.Println("Filtered Employees:", filtered) }
输出
Filtered Employees: [{Aman 50000} {Akshay 60000}]
示例 2
在下面的代码中,“filterEmployees”函数根据给定的过滤器筛选员工列表,而“sallaryFilter()”函数返回一个用于根据薪资筛选员工的过滤器函数。
package main import "fmt" type Employee struct { Name string Salary int } type FilterFunc func(Employee) bool func FilterEmployees(employees []Employee, filterFunc FilterFunc) []Employee { var filteredEmployees []Employee for _, employee := range employees { if filterFunc(employee) { filteredEmployees = append(filteredEmployees, employee) } } return filteredEmployees } func SalaryFilter(minSalary, maxSalary int) FilterFunc { return func(employee Employee) bool { return employee.Salary >= minSalary && employee.Salary <= maxSalary } } func main() { employees := []Employee{ {Name: "Sam", Salary: 50000}, {Name: "Akhil", Salary: 70000}, {Name: "Akshay", Salary: 40000}, {Name: "Summer", Salary: 80000}, } salaryFilter := SalaryFilter(45000, 85000) filtered := FilterEmployees(employees, salaryFilter) fmt.Println("Filtered Employees:", filtered) }
输出
Filtered Employees: [{Sam 50000} {Akhil 70000} {Summer 80000}]
示例 3
下面的代码为每个员工创建一个goroutine,以异步方式根据给定的薪资筛选他们,并将筛选后的员工通过通道发送,主函数接收筛选后的员工并返回最终结果。
package main import ( "fmt" "time" ) type Employee struct { Name string Salary int } func FilterEmployeesConcurrently(employees []Employee, minSalary, maxSalary int) []Employee { var filteredEmployees []Employee employeeChannel := make(chan Employee) for _, employee := range employees { go func(e Employee) { if e.Salary >= minSalary && e.Salary <= maxSalary { employeeChannel <- e } }(employee) } // Collect filtered employees from the channel go func() { for employee := range employeeChannel { filteredEmployees = append(filteredEmployees, employee) } }() time.Sleep(time.Millisecond) return filteredEmployees } func main() { employees := []Employee{ {Name: "Arya", Salary: 5000}, {Name: "Sansa", Salary: 7000}, {Name: "Rob", Salary: 4000}, {Name: "John", Salary: 6000}, } filtered := FilterEmployeesConcurrently(employees, 4500, 6500) fmt.Println("Filtered Employees:", filtered) }
输出
Filtered Employees: [{Arya 5000} {John 6000}]
结论
在本文中,我们讨论了如何在Go语言中根据员工薪资创建员工过滤器。我们探索了三种不同的方法:使用goroutine和通道、传统的循环方法和函数式方法。
广告