Golang program that takes in a slice of integers and an anonymous function that filters each element in the slice


In this Go language article, we will write programs that take in a slice of integers and an anonymous function that filters each element in the slice. An anonymous function is the function which uses no function name and is called by the variable which is assigned to it. It is used generally used with event listeners. Here, Filter function will be created with the anonymous functions to filter the values from slice.

Syntax

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

func range(variable)

The range function is used to iterate over any data type. To use this, we first have to write the range keyword followed by the data type to which we want to iterate and as a result the loop will iterate till the last element of the variable.

Algorithm

  • This program imports main and fmt package as necessary packages.

  • Create a main function.

  • In the main, create an integer slice named values and initialize it with the values.

  • In this step, call the Filter function with the values slice and a filtering anonymous function which checks whether the elements are even or not.

  • Store the values in a variable named filtered_values.

  • In this step, create the Filter function that takes slice values and a function (fn) as arguments and returns a new filtered slice of integers.

  • In this step, create an empty slice filtered_values of type int.

  • Then, use a for loop Iterate over each value in the values slice.

  • Then, check if the fn function returns true for the current value.

  • If the fn function returns true, append the value to the filtered_values slice using the append function.

  • After the loop terminates, return the filtered_values slice.

Example

In this example, we will write a Golang program that filters each element in the slice by using filter and anonymous functions. The filtered values are appended to the slice.

package main

import (
	"fmt"
)
func main() {
	values := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	fmt.Println("The intial values given here are:", values)

	filtered_values := Filter(values, func(n int) bool {
		return n%2 == 0
	})

	fmt.Println("The filtered numbers are:")
	fmt.Println(filtered_values)
}

func Filter(values []int, fn func(int) bool) []int {
	filtered_values := []int{}

	for _, val := range values {
		if fn(val) {
			filtered_values = append(filtered_values, val)
		}
	}

	return filtered_values
}

Output

The initial values given here are: [1 2 3 4 5 6 7 8 9 10]
The filtered numbers are:
[2 4 6 8 10]

Conclusion

We compiled and executed the program of taking in a slice of integers and an anonymous function that filters each element using an example that uses filter function.

Updated on: 04-Aug-2023

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements