- 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
Go Language allows you to apply a filter on particular data for analyzing specified data, work on a particular property of data, data integration and more.In this article we are going to write a program to create a filter for the employees, using iterative filtering, functional filtering, as well as using go language built in filtering function.
Syntax
filtered := funk.Filter(collection, func(item Type) bool {…})
collection = It is the original collection to filter.
This function takes two arguments, a collection and a filtering function.
Algorithm
Make an array or slice named "employees" of the Employee type.
Set up the "employees" slice with a collection of Employee instances that include their names, ages, and departments.
Call the filterEmployeesByAge function, supplying as arguments the "employees" slice, minimum age, and maximum age.
Within the method filterEmployeesByAge make an empty slice of type Employee named "filteredEmployees". Determine if each employee's age is higher than or equal to the minimum age and less than or equal to the maximum age. Add the employee to the "filteredEmployees" slice if the age is within the provided range. Get the "filteredEmployees" slice back.
Set the variable "filtered" to the result of filterEmployeesByAge.
"Filtered Employees:" should be printed. Iterate through all of the employees in the "filtered" slice.
Print the name of each employee. Put an end to the main function.
Example 1
In this method we are going to use the iterative approach to filter the employee data. This method is more suitable when you need to get more control over the filtering process.
package main import "fmt" type Employee struct { Name string Age int Department string } func filterEmployeesByAge(employees []Employee, minAge, maxAge int) []Employee { var filteredEmployees []Employee for _, emp := range employees { if emp.Age >= minAge && emp.Age <= maxAge { filteredEmployees = append(filteredEmployees, emp) } } return filteredEmployees } func main() { employees := []Employee{ {Name: "Akhil", Age: 24, Department: "Engineering"}, {Name: "Akshay", Age: 28, Department: "Sales"}, {Name: "Sahil", Age: 23, Department: "Marketing"}, } filtered := filterEmployeesByAge(employees, 24, 45) fmt.Println("Filtered Employees:") for _, emp := range filtered { fmt.Println(emp.Name) } }
Output
Filtered Employees: Akhil Akshay
Example 2
In this method we are going to use higher level function to filter the employees. This method involves higher level function like “filter” to apply filtering function.
package main import "fmt" type Employee struct { Name string Age int Department string } type Predicate func(Employee) bool func filterEmployees(employees []Employee, predicate Predicate) []Employee { var filteredEmployees []Employee for _, emp := range employees { if predicate(emp) { filteredEmployees = append(filteredEmployees, emp) } } return filteredEmployees } func main() { employees := []Employee{ {Name: "Akhil", Age: 24, Department: "Engineering"}, {Name: "Akshay", Age: 32, Department: "Sales"}, {Name: "Nitin", Age: 28, Department: "Marketing"}, } filtered := filterEmployees(employees, func(emp Employee) bool { return emp.Age>= 30 && emp.Age <= 45 }) fmt.Println("Filtered Employees:") for _, emp := range filtered { fmt.Println(emp.Name) } }
Output
Filtered Employees: Akshay
Example 3
In the code given below we will use the “go-funk” library to filter a slice of employees based on a specific age.
package main import ( "fmt" "github.com/thoas/go-funk" ) type Employee struct { Name string Age int Department string } func main() { employees := []Employee{ {Name: "Akhil", Age: 28, Department: "Engineering"}, {Name: "Akshay", Age: 32, Department: "Sales"}, {Name: "Sahil", Age: 40, Department: "Marketing"}, } filtered := funk.Filter(employees, func(emp Employee) bool { return emp.Age >= 30 && emp.Age <= 45 }).([]Employee) fmt.Println("Filtered Employees:") for _, emp := range filtered { fmt.Println(emp.Name) } }
Output
Filtered Employees: Akshay Sahil
Conclusion
In this article we have discussed how we can create a filter for employees in go language. We have explored different Data filtering techniques including a built-in function called funk.filter(), functional filtering method as well as iterative filtering. Filtering data is very much useful when you want to work on a particular property, data analyzing, and more.