- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Create Filter for the Employees Based on their Salary
It is important to have the knowledge of filters while working on data sets in go language because there may be cases where you want to analyze data to get customized results. In this article we are going to create a filter on the employee list based on the salary using traditional looping method, functions approach as well as using goroutines.
Example 1
In the code given below “FilterEmployeeBySallary()” filters the list of employee based on a salary range and returns the list of employee that falls in that salary range.
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) }
Output
Filtered Employees: [{Aman 50000} {Akshay 60000}]
Example 2
In the code given below “filterEmployees” function filters the list of employees based on a given filter and sallaryFilter() function returns a filter function to filter employees on salary.
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) }
Output
Filtered Employees: [{Sam 50000} {Akhil 70000} {Summer 80000}]
Example 3
The code given below creates a goroutine for each employee to asynchronously filter them based on given salary and send filtered employees through a channel, the main function takes the filtered employee and returns the final result.
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) }
Output
Filtered Employees: [{Arya 5000} {John 6000}]
Conclusion
In this article we have discussed how we can create a filter for the employee based on their salary in golanguage.Here we have explored three different methods using goroutines and channels, traditional looping and a functional approach.