Free preview.You're sampling one lesson — enroll free to unlock all 10 lessons and track your progress.
Enroll free
lesson

Closures and Higher-Order Functions

Closures and Higher-Order Functions

In this lesson — part of Advanced Features — you'll learn closures and higher-order functions in Go and why it matters in real work.

Why it matters

Functions that take or return functions unlock concise, composable code.

Key ideas

  • Functions as values
  • map / filter / reduce
  • Callbacks
  • Composition

In practice

Here's how it looks in idiomatic Go:

package main

import "fmt"

func main() {
	nums := []int{1, 2, 3, 4}
	var doubledEvens []int
	for _, n := range nums {
		if n%2 == 0 { // filter
			doubledEvens = append(doubledEvens, n*2) // map
		}
	}
	fmt.Println(doubledEvens) // [4 8]
}

Go note: Go's standard library has no generic map/filter, so the idiomatic approach is an explicit range loop with append.

Try it yourself

Exercise: In Go, use map and filter to get the squares of the even numbers.

Recap

You now understand closures and higher-order functions and can apply it in Go. Mark this lesson complete and continue to the next one.