What Are Filter, Closure, And Custom Functions In Go?

ยท

3 min read

Go is a powerful programming language that offers a wide range of features to help developers write clean, efficient, and maintainable code. Among these features are filter, closure, and custom functions, which are used to manipulate and process data in various ways. In this article, we'll explore each of these functions and how they work in Go with examples.

Filter Functions:

Filter functions are used to select and extract elements from a collection based on specific criteria. In Go, you can use the filter() function to filter elements from a slice or array. Here's an example of how to use filter() function in Go:

func filter(numbers []int, criteria func(int) bool) []int {
    var result []int
    for _, number := range numbers {
        if criteria(number) {
            result = append(result, number)
        }
    }
    return result
}

func main() {
    numbers := []int{1, 2, 3, 4, 5}
    result := filter(numbers, func(n int) bool {
        return n%2 == 0
    })
    fmt.Println(result)
}

In the above example, we define a filter() function that takes a slice of integers and a criteria function as parameters. The filter() function iterates through each element in the slice and checks if it satisfies the criteria. If the criteria are met, the element is added to a new slice that is returned as the result.

Closure Functions:

Closure functions are used to create functions that can access and modify variables that are not in their immediate scope. In Go, you can create a closure function by defining a function inside another function. Here's an example of how to create a closure function in Go:

func counter() func() int {
    i := 0
    return func() int {
        i++
        return i
    }
}

func main() {
    c1 := counter()
    fmt.Println(c1()) // 1
    fmt.Println(c1()) // 2

    c2 := counter()
    fmt.Println(c2()) // 1
}

In the above example, we define a counter() function that returns a closure function. The closure function increments and returns a variable that is defined in the outer function. When we call counter() function multiple times, we get separate instances of the closure function that maintain their own state.

Custom Functions:

Custom functions are used to perform a specific task or set of tasks that are not provided by the built-in functions of Go. In Go, you can define custom functions using the func keyword. Here's an example of how to define a custom function in Go:

func sum(numbers ...int) int {
    total := 0
    for _, number := range numbers {
        total += number
    }
    return total
}

func main() {
    result := sum(1, 2, 3, 4, 5)
    fmt.Println(result) // 15
}

In the above example, we define a sum() function that takes a variable number of arguments and returns their sum. We can call this function with any number of arguments, and it will calculate their sum and return the result.

Conclusion:

Filter, closure, and custom functions are powerful tools that can help you write cleaner, more efficient, and more maintainable code in Go. By understanding how these functions work and how to use them, you can write better code that is easier to read, understand, and maintain.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!

ย