Fundamentals 7 min read

Master Go Functions: Variables, Higher‑Order Functions, and Closures

This article walks through Go function fundamentals, showing how functions can be treated as variables, how to define function types, and demonstrating higher‑order functions, anonymous functions, and closures with clear code examples and visual outputs.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Go Functions: Variables, Higher‑Order Functions, and Closures

Preface

Hey everyone, I’m 星期八. In this article we’ll finish the remaining concepts about Go functions, building on the previous two posts about Go function basics.

Recap

We previously explained how a function’s memory is allocated on the stack.

Function and Variable

Function name as a variable

You can assign a function to a variable and call it through that variable.

package main

import "fmt"

func say() {
    fmt.Println("say")
}

func main() {
    var s1 = say
    s1()
}

The program prints the string "say".

Using fmt.Printf("%T\n", s1) shows the variable’s type. If the function signature changes, the type changes accordingly.

func say(s int) int {
    fmt.Println("say")
    return 1
}

fmt.Printf("%T
", s1)

Defining Function Types

Use the type keyword to create a named function type, which restricts the variable to functions with a specific signature.

type a func()
type b func(int)
type c func(int, int) int

Example:

package main

import "fmt"

type cType func(int, int) int

func say1(a, b int) int {
    fmt.Println("say", a+b)
    return 1
}

func say2(a, b int) {
    fmt.Println("say")
}

func main() {
    var s1 cType
    s1 = say1 // s1 now refers to say1
    s1(1, 1)
    // var s2 cType
    // s2 = say2 // compile error: cannot use say2 (type func(int, int)) as type cType
}

Higher‑Order Functions

Higher‑order functions treat functions as parameters or return values.

Function as a parameter

package main

import "fmt"

func add(x int, y int) int { return x + y }

func calc(x int, y int, other func(int, int) int) int { return other(x, y) }

func main() {
    var result = calc(34, 12, add)
    fmt.Println(result)
}

Function as a return value

package main

import "fmt"

func add(x int, y int) int { return x + y }

func test() func(int, int) int { return add }

func main() {
    var a = test()
    fmt.Println(a(1, 2))
}

The author notes that these patterns are not frequently used in everyday code.

Anonymous Functions

An anonymous function has no name and can be defined and invoked inline.

func main() {
    var s1 = func(x int, y int) (int) { return x + y }(1, 2)
    fmt.Println(s1)
}

Closures

A closure is a function that captures variables from its surrounding scope.

package main

import "fmt"

func other() func() {
    var a = 666
    return func() { fmt.Println(a) }
}

func main() {
    var o = other()
    o()
}

The output shows the captured variable remains alive.

Summary

This article covered Go functions and variables, higher‑order functions, anonymous functions, and closures, emphasizing that closures can be tricky and require practice to master.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GofunctionsHigher-Order FunctionsAnonymous Functions
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.