跪拜 Guibai
← All articles
Frontend · Backend · Go

Go Closures and Higher-Order Functions Work Just Like JavaScript

By 他们叫我秃子 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A frontend engineer moving to Go can treat closures and higher-order functions as transferable skills rather than new concepts, which cuts the learning time for function-level abstractions and state encapsulation patterns. The mental models from JavaScript callbacks, factory functions, and IIFEs map cleanly onto Go, so the real work is memorizing Go's type-declaration order and the absence of arrow-function sugar.

Summary

A frontend developer learning Go discovers that anonymous functions, closures, and higher-order functions are nearly identical in concept to their JavaScript counterparts. Anonymous functions can be assigned to variables, executed immediately as IIFEs, and passed as arguments. Closures capture outer-scope variables and retain state across calls, with each invocation of a factory function producing an independent state scope. Functions can be returned from other functions and passed as parameters, enabling callback-style patterns and custom implementations of familiar array methods like filter. The core distinction is that anonymous functions, closures, function parameters, and higher-order functions are separate concepts that often appear together but address different concerns: naming, state capture, value passing, and function-level abstraction. Type aliases for function signatures provide readability when function types grow complex. The learning curve flattens sharply here because the underlying programming ideas transfer directly from JavaScript, leaving only Go's specific syntax to absorb.

Takeaways
Anonymous functions in Go use the `func(args) returns` literal syntax and can be assigned to variables, called immediately, or passed as arguments.
A closure forms when an anonymous function references a variable from its enclosing scope; the function retains access to that variable even after the enclosing function returns.
Each call to a closure-producing factory function like `createCounter` allocates an independent copy of the captured state; separate counters do not interfere.
Functions can be passed as parameters by specifying a full function signature type, such as `operation func(int, int) int`, and the compiler enforces signature compatibility.
Higher-order functions are those that either accept a function as a parameter or return a function; Go supports both patterns natively.
Type aliases like `type Operation func(int, int) int` can name complex function signatures to improve readability when the same signature repeats.
Anonymous functions, closures, function parameters, and higher-order functions are distinct concepts that frequently combine: a function can be anonymous without being a closure, and a named function can be passed as an argument without involving closures.
Conclusions

The conceptual overlap between Go and JavaScript on closures is so complete that a frontend developer's existing intuition about lexical scoping, state factories, and callbacks transfers with almost no friction; the only real adjustment is Go's type-declaration order and the lack of arrow-function shorthand.

The article's filter implementation exposes a pedagogical gap: the author uses slices, `range`, and `append` before formally introducing them, which mirrors the real experience of learning a language where practical need pulls you into unfamiliar territory ahead of the curriculum.

Go's requirement that passed-in function signatures match exactly — parameter count, types, and return types — is stricter than JavaScript's runtime duck-typing of callbacks, and this compile-time enforcement catches a class of bugs that JavaScript developers typically discover only at runtime.

Concepts & terms
Anonymous function
A function literal without a name, written as `func(args) returns { body }`. It can be assigned to a variable, passed as an argument, or invoked immediately.
Closure
A function that references variables from its enclosing lexical scope. The function retains access to those variables even after the enclosing function has returned, and each invocation of the enclosing function creates a fresh set of captured variables.
Higher-order function
A function that either accepts another function as a parameter or returns a function as its result. Both `calculate` (accepts a function) and `createCounter` (returns a function) qualify.
Function type / signature
A type describing a function's parameter and return types, such as `func(int, int) int`. Go uses these signatures for parameter and variable declarations, and the compiler checks that assigned or passed functions match exactly.
IIFE (Immediately Invoked Function Expression)
A function defined and called in a single expression. In Go, the syntax is `func(args) { body }(args)`; the trailing parentheses invoke the function immediately without assigning it to a variable.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗