Go Language Advanced Functions

After mastering the basics of Go functions, this chapter dives into advanced function features, including closures, anonymous functions, variadic parameters, higher-order functions, and more.

🎯 Anonymous Functions

Anonymous Function Basics

package main

import "fmt"

func main() {
    // 定义并立即调用匿名函数
    func() {
        fmt.Println("这是一个匿名函数")
    }()
    
    // 将匿名函数赋值给变量
    greet := func(name string) {
        fmt.Printf("你好, %s!\n", name)
    }
    greet("张三")
    
    // 带返回值的匿名函数
    add := func(a, b int) int {
        return a + b
    }
    result := add(5, 3)
    fmt.Printf("5 + 3 = %d\n", result)
    
    // 多返回值匿名函数
    divide := func(a, b float64) (float64, error) {
        if b == 0 {
            return 0, fmt.Errorf("除数不能为零")
        }
        return a / b, nil
    }
    
    if quotient, err := divide(10, 2); err == nil {
        fmt.Printf("10 / 2 = %.2f\n", quotient)
    } else {
        fmt.Printf("错误: %v\n", err)
    }
}

Functions as Parameters

import (
    "fmt"
    "math"
)

// 定义函数类型
type MathOperation func(float64, float64) float64

// 高阶函数:接受函数作为参数
func calculator(a, b float64, operation MathOperation) float64 {
    return operation(a, b)
}

// 数学运算函数
func add(a, b float64) float64 {
    return a + b
}

func multiply(a, b float64) float64 {
    return a * b
}

func power(base, exp float64) float64 {
    return math.Pow(base, exp)
}

func main() {
    x, y := 5.0, 3.0
    
    // 传递命名函数
    fmt.Printf("%.1f + %.1f = %.1f\n", x, y, calculator(x, y, add))
    fmt.Printf("%.1f * %.1f = %.1f\n", x, y, calculator(x, y, multiply))
    fmt.Printf("%.1f ^ %.1f = %.1f\n", x, y, calculator(x, y, power))
    
    // 传递匿名函数
    fmt.Printf("%.1f - %.1f = %.1f\n", x, y, calculator(x, y, func(a, b float64) float64 {
        return a - b
    }))
    
    // 传递除法函数(带错误处理)
    divide := func(a, b float64) float64 {
        if b == 0 {
            fmt.Println("警告: 除数为零,返回无穷大")
            return math.Inf(1)
        }
        return a / b
    }
    fmt.Printf("%.1f / %.1f = %.1f\n", x, y, calculator(x, y, divide))
}

🔒 Closures

Closure Basics

func main() {
    // 闭包捕获外部变量
    counter := func() func() int {
        count := 0
        return func() int {
            count++
            return count
        }
    }()
    
    fmt.Println("计数器演示:")
    fmt.Println(counter()) // 1
    fmt.Println(counter()) // 2
    fmt.Println(counter()) // 3
    
    // 每个闭包都有自己的变量副本
    counter2 := func() func() int {
        count := 0
        return func() int {
            count++
            return count
        }
    }()
    
    fmt.Println("第二个计数器:")
    fmt.Println(counter2()) // 1
    fmt.Println(counter())  // 4 (第一个计数器继续)
    fmt.Println(counter2()) // 2
}

Practical Applications of Closures

import (
    "fmt"
    "strings"
)

// 创建配置闭包
func createLogger(prefix string) func(string) {
    return func(message string) {
        fmt.Printf("[%s] %s\n", prefix, message)
    }
}

// 创建验证器闭包
func createValidator(minLength int) func(string) bool {
    return func(input string) bool {
        return len(strings.TrimSpace(input)) >= minLength
    }
}

// 创建累加器闭包
func createAccumulator(initial int) func(int) int {
    sum := initial
    return func(value int) int {
        sum += value
        return sum
    }
}

// 创建配置函数生成器
func createMultiplier(factor int) func(int) int {
    return func(n int) int {
        return n * factor
    }
}

func main() {
    // 日志器示例
    errorLogger := createLogger("ERROR")
    infoLogger := createLogger("INFO")
    
    errorLogger("数据库连接失败")
    infoLogger("应用启动成功")
    
    // 验证器示例
    passwordValidator := createValidator(8)
    usernameValidator := createValidator(3)
    
    fmt.Printf("密码 'abc' 有效: %v\n", passwordValidator("abc"))        // false
    fmt.Printf("密码 'password123' 有效: %v\n", passwordValidator("password123")) // true
    fmt.Printf("用户名 'jo' 有效: %v\n", usernameValidator("jo"))           // false
    fmt.Printf("用户名 'john' 有效: %v\n", usernameValidator("john"))       // true
    
    // 累加器示例
    acc := createAccumulator(10)
    fmt.Printf("累加器初始值: %d\n", acc(0))  // 10
    fmt.Printf("累加5: %d\n", acc(5))        // 15
    fmt.Printf("累加3: %d\n", acc(3))        // 18
    
    // 乘法器示例
    double := createMultiplier(2)
    triple := createMultiplier(3)
    
    fmt.Printf("5 的双倍: %d\n", double(5))  // 10
    fmt.Printf("5 的三倍: %d\n", triple(5))  // 15
}

🔄 Variadic Functions

Variadic Function Basics

import (
    "fmt"
    "strings"
)

// 计算整数之和
func sum(numbers ...int) int {
    total := 0
    for _, num := range numbers {
        total += num
    }
    return total
}

// 查找最大值
func max(numbers ...int) int {
    if len(numbers) == 0 {
        return 0
    }
    
    maximum := numbers[0]
    for _, num := range numbers[1:] {
        if num > maximum {
            maximum = num
        }
    }
    return maximum
}

// 字符串连接
func concat(separator string, strs ...string) string {
    return strings.Join(strs, separator)
}

// 格式化输出
func printf(format string, args ...interface{}) {
    fmt.Printf(format+"\n", args...)
}

func main() {
    // 可变参数调用
    fmt.Printf("无参数求和: %d\n", sum())                    // 0
    fmt.Printf("单参数求和: %d\n", sum(5))                   // 5
    fmt.Printf("多参数求和: %d\n", sum(1, 2, 3, 4, 5))       // 15
    
    // 传递切片作为可变参数
    numbers := []int{10, 20, 30, 40}
    fmt.Printf("切片求和: %d\n", sum(numbers...))           // 100
    
    // 最大值示例
    fmt.Printf("最大值: %d\n", max(3, 7, 2, 9, 1))         // 9
    
    // 字符串连接
    result := concat(" | ", "Go", "Python", "Java", "C++")
    fmt.Printf("连接结果: %s\n", result)
    
    // 格式化输出
    printf("姓名: %s, 年龄: %d, 成绩: %.2f", "张三", 25, 95.5)
    
    // 混合固定参数和可变参数
    words := []string{"Hello", "World", "Go", "Language"}
    fmt.Printf("用空格连接: %s\n", concat(" ", words...))
}

Advanced Variadic Usage

import (
    "fmt"
    "reflect"
)

// 通用打印函数
func print(title string, values ...interface{}) {
    fmt.Printf("=== %s ===\n", title)
    for i, value := range values {
        fmt.Printf("%d: %v (类型: %s)\n", i+1, value, reflect.TypeOf(value))
    }
    fmt.Println()
}

// 配置选项模式
type Config struct {
    Host    string
    Port    int
    Timeout int
    Debug   bool
}

type Option func(*Config)

func WithHost(host string) Option {
    return func(c *Config) {
        c.Host = host
    }
}

func WithPort(port int) Option {
    return func(c *Config) {
        c.Port = port
    }
}

func WithTimeout(timeout int) Option {
    return func(c *Config) {
        c.Timeout = timeout
    }
}

func WithDebug(debug bool) Option {
    return func(c *Config) {
        c.Debug = debug
    }
}

func NewConfig(options ...Option) *Config {
    // 默认配置
    config := &Config{
        Host:    "localhost",
        Port:    8080,
        Timeout: 30,
        Debug:   false,
    }
    
    // 应用选项
    for _, option := range options {
        option(config)
    }
    
    return config
}

func main() {
    // 通用打印示例
    print("混合数据类型", 42, "Hello", 3.14, true, []int{1, 2, 3})
    
    // 配置选项模式示例
    config1 := NewConfig()
    fmt.Printf("默认配置: %+v\n", config1)
    
    config2 := NewConfig(
        WithHost("example.com"),
        WithPort(9000),
        WithDebug(true),
    )
    fmt.Printf("自定义配置: %+v\n", config2)
    
    config3 := NewConfig(
        WithTimeout(60),
        WithDebug(true),
    )
    fmt.Printf("部分自定义配置: %+v\n", config3)
}

🎯 Higher-Order Functions

Functions as Return Values

import (
    "fmt"
    "math"
)

// 返回函数的函数
func createMathFunction(operation string) func(float64, float64) float64 {
    switch operation {
    case "add":
        return func(a, b float64) float64 { return a + b }
    case "subtract":
        return func(a, b float64) float64 { return a - b }
    case "multiply":
        return func(a, b float64) float64 { return a * b }
    case "divide":
        return func(a, b float64) float64 {
            if b != 0 {
                return a / b
            }
            return math.NaN()
        }
    case "power":
        return func(a, b float64) float64 { return math.Pow(a, b) }
    default:
        return func(a, b float64) float64 { return 0 }
    }
}

// 创建条件函数
func createCondition(operator string, threshold float64) func(float64) bool {
    switch operator {
    case ">":
        return func(value float64) bool { return value > threshold }
    case ">=":
        return func(value float64) bool { return value >= threshold }
    case "<":
        return func(value float64) bool { return value < threshold }
    case "<=":
        return func(value float64) bool { return value <= threshold }
    case "==":
        return func(value float64) bool { return value == threshold }
    default:
        return func(value float64) bool { return false }
    }
}

// 数组过滤器
func filter(numbers []float64, condition func(float64) bool) []float64 {
    var result []float64
    for _, num := range numbers {
        if condition(num) {
            result = append(result, num)
        }
    }
    return result
}

func main() {
    // 创建数学函数
    add := createMathFunction("add")
    multiply := createMathFunction("multiply")
    power := createMathFunction("power")
    
    fmt.Printf("5 + 3 = %.2f\n", add(5, 3))
    fmt.Printf("4 * 6 = %.2f\n", multiply(4, 6))
    fmt.Printf("2 ^ 3 = %.2f\n", power(2, 3))
    
    // 创建条件函数
    isPositive := createCondition(">", 0)
    isLarge := createCondition(">=", 100)
    
    numbers := []float64{-5, 10, 50, 100, 150, -20, 75}
    
    fmt.Printf("原始数组: %v\n", numbers)
    fmt.Printf("正数: %v\n", filter(numbers, isPositive))
    fmt.Printf("大于等于100: %v\n", filter(numbers, isLarge))
    
    // 使用匿名函数作为条件
    evenNumbers := filter(numbers, func(n float64) bool {
        return int(n)%2 == 0
    })
    fmt.Printf("偶数: %v\n", evenNumbers)
}

Function Composition

import (
    "fmt"
    "strings"
)

// 函数类型定义
type StringProcessor func(string) string
type NumberProcessor func(int) int

// 字符串处理函数组合
func composeString(functions ...StringProcessor) StringProcessor {
    return func(input string) string {
        result := input
        for _, fn := range functions {
            result = fn(result)
        }
        return result
    }
}

// 数字处理函数组合
func composeNumber(functions ...NumberProcessor) NumberProcessor {
    return func(input int) int {
        result := input
        for _, fn := range functions {
            result = fn(result)
        }
        return result
    }
}

// 字符串处理函数
func toUpper(s string) string {
    return strings.ToUpper(s)
}

func addPrefix(s string) string {
    return "前缀-" + s
}

func addSuffix(s string) string {
    return s + "-后缀"
}

func removeSpaces(s string) string {
    return strings.ReplaceAll(s, " ", "")
}

// 数字处理函数
func double(n int) int {
    return n * 2
}

func addTen(n int) int {
    return n + 10
}

func square(n int) int {
    return n * n
}

func main() {
    // 字符串处理组合
    processor1 := composeString(
        strings.TrimSpace,
        toUpper,
        addPrefix,
        addSuffix,
    )
    
    result1 := processor1("  hello world  ")
    fmt.Printf("字符串处理结果: '%s'\n", result1)
    
    processor2 := composeString(
        removeSpaces,
        toUpper,
        addPrefix,
    )
    
    result2 := processor2("go language tutorial")
    fmt.Printf("字符串处理结果2: '%s'\n", result2)
    
    // 数字处理组合
    numberProcessor1 := composeNumber(
        double,
        addTen,
        square,
    )
    
    result3 := numberProcessor1(5) // ((5*2)+10)^2 = 20^2 = 400
    fmt.Printf("数字处理结果: %d\n", result3)
    
    numberProcessor2 := composeNumber(
        square,
        double,
        addTen,
    )
    
    result4 := numberProcessor2(3) // ((3^2)*2)+10 = (9*2)+10 = 28
    fmt.Printf("数字处理结果2: %d\n", result4)
}

🎭 Advanced Recursive Functions

Tail Recursion Optimization Simulation

import "fmt"

// 传统递归(可能导致栈溢出)
func factorial(n int) int {
    if n <= 1 {
        return 1
    }
    return n * factorial(n-1)
}

// 尾递归风格(Go 不做尾递归优化,但这是更好的风格)
func factorialTail(n int) int {
    return factorialHelper(n, 1)
}

func factorialHelper(n, acc int) int {
    if n <= 1 {
        return acc
    }
    return factorialHelper(n-1, acc*n)
}

// 斐波那契数列的不同实现
func fibonacciNaive(n int) int {
    if n <= 1 {
        return n
    }
    return fibonacciNaive(n-1) + fibonacciNaive(n-2)
}

func fibonacciMemo(n int) int {
    memo := make(map[int]int)
    return fibonacciMemoHelper(n, memo)
}

func fibonacciMemoHelper(n int, memo map[int]int) int {
    if n <= 1 {
        return n
    }
    
    if val, exists := memo[n]; exists {
        return val
    }
    
    memo[n] = fibonacciMemoHelper(n-1, memo) + fibonacciMemoHelper(n-2, memo)
    return memo[n]
}

// 迭代版本(通常更高效)
func fibonacciIterative(n int) int {
    if n <= 1 {
        return n
    }
    
    a, b := 0, 1
    for i := 2; i <= n; i++ {
        a, b = b, a+b
    }
    return b
}

func main() {
    // 阶乘比较
    n := 10
    fmt.Printf("传统递归阶乘 %d! = %d\n", n, factorial(n))
    fmt.Printf("尾递归风格阶乘 %d! = %d\n", n, factorialTail(n))
    
    // 斐波那契比较
    fib := 10
    fmt.Printf("朴素递归斐波那契 F(%d) = %d\n", fib, fibonacciNaive(fib))
    fmt.Printf("记忆化递归斐波那契 F(%d) = %d\n", fib, fibonacciMemo(fib))
    fmt.Printf("迭代斐波那契 F(%d) = %d\n", fib, fibonacciIterative(fib))
    
    // 性能比较(较大的数)
    bigFib := 30
    fmt.Printf("\n较大数字的斐波那契 F(%d):\n", bigFib)
    fmt.Printf("记忆化递归: %d\n", fibonacciMemo(bigFib))
    fmt.Printf("迭代实现: %d\n", fibonacciIterative(bigFib))
    // 注意:不运行朴素递归版本,因为会很慢
}

🔍 Function Types and Methods

Function Types as Struct Fields

import "fmt"

// 定义函数类型
type Operation func(int, int) int
type Validator func(interface{}) bool
type Formatter func(interface{}) string

// 计算器结构体
type Calculator struct {
    name      string
    operation Operation
}

func (c Calculator) Calculate(a, b int) int {
    return c.operation(a, b)
}

func (c Calculator) String() string {
    return fmt.Sprintf("计算器: %s", c.name)
}

// 数据处理器结构体
type DataProcessor struct {
    validator Validator
    formatter Formatter
}

func (dp DataProcessor) Process(data interface{}) (string, bool) {
    if !dp.validator(data) {
        return "", false
    }
    return dp.formatter(data), true
}

func main() {
    // 创建不同的计算器
    adder := Calculator{
        name: "加法器",
        operation: func(a, b int) int {
            return a + b
        },
    }
    
    multiplier := Calculator{
        name: "乘法器",
        operation: func(a, b int) int {
            return a * b
        },
    }
    
    fmt.Printf("%s: 5 + 3 = %d\n", adder, adder.Calculate(5, 3))
    fmt.Printf("%s: 5 * 3 = %d\n", multiplier, multiplier.Calculate(5, 3))
    
    // 创建数据处理器
    stringProcessor := DataProcessor{
        validator: func(data interface{}) bool {
            _, ok := data.(string)
            return ok
        },
        formatter: func(data interface{}) string {
            return fmt.Sprintf("字符串: '%s'", data)
        },
    }
    
    numberProcessor := DataProcessor{
        validator: func(data interface{}) bool {
            _, ok := data.(int)
            return ok
        },
        formatter: func(data interface{}) string {
            return fmt.Sprintf("数字: %d", data)
        },
    }
    
    // 测试数据处理
    testData := []interface{}{"Hello", 42, 3.14, true}
    
    for _, data := range testData {
        if result, ok := stringProcessor.Process(data); ok {
            fmt.Println(result)
        } else if result, ok := numberProcessor.Process(data); ok {
            fmt.Println(result)
        } else {
            fmt.Printf("无法处理数据: %v (类型: %T)\n", data, data)
        }
    }
}

🎓 Practical Application Examples

Event Handling System

import (
    "fmt"
    "sync"
    "time"
)

// 事件类型
type Event struct {
    Type string
    Data interface{}
    Time time.Time
}

// 事件处理函数类型
type EventHandler func(Event)

// 事件总线
type EventBus struct {
    handlers map[string][]EventHandler
    mutex    sync.RWMutex
}

func NewEventBus() *EventBus {
    return &EventBus{
        handlers: make(map[string][]EventHandler),
    }
}

// 注册事件处理器
func (eb *EventBus) Subscribe(eventType string, handler EventHandler) {
    eb.mutex.Lock()
    defer eb.mutex.Unlock()
    
    eb.handlers[eventType] = append(eb.handlers[eventType], handler)
}

// 发布事件
func (eb *EventBus) Publish(eventType string, data interface{}) {
    eb.mutex.RLock()
    handlers := eb.handlers[eventType]
    eb.mutex.RUnlock()
    
    event := Event{
        Type: eventType,
        Data: data,
        Time: time.Now(),
    }
    
    for _, handler := range handlers {
        go handler(event) // 异步处理
    }
}

// 中间件类型
type Middleware func(EventHandler) EventHandler

// 应用中间件
func (eb *EventBus) Use(eventType string, middleware Middleware) {
    eb.mutex.Lock()
    defer eb.mutex.Unlock()
    
    handlers := eb.handlers[eventType]
    for i, handler := range handlers {
        handlers[i] = middleware(handler)
    }
}

// 日志中间件
func LoggingMiddleware(handler EventHandler) EventHandler {
    return func(event Event) {
        start := time.Now()
        fmt.Printf("[LOG] 开始处理事件: %s\n", event.Type)
        
        handler(event)
        
        duration := time.Since(start)
        fmt.Printf("[LOG] 事件处理完成: %s (耗时: %v)\n", event.Type, duration)
    }
}

// 错误处理中间件
func ErrorHandlingMiddleware(handler EventHandler) EventHandler {
    return func(event Event) {
        defer func() {
            if r := recover(); r != nil {
                fmt.Printf("[ERROR] 事件处理异常: %s, 错误: %v\n", event.Type, r)
            }
        }()
        
        handler(event)
    }
}

func main() {
    bus := NewEventBus()
    
    // 注册用户相关事件处理器
    bus.Subscribe("user.created", func(event Event) {
        user := event.Data.(map[string]interface{})
        fmt.Printf("新用户创建: %s (邮箱: %s)\n", user["name"], user["email"])
        
        // 模拟发送欢迎邮件
        time.Sleep(100 * time.Millisecond)
        fmt.Printf("欢迎邮件已发送给: %s\n", user["name"])
    })
    
    bus.Subscribe("user.created", func(event Event) {
        user := event.Data.(map[string]interface{})
        fmt.Printf("用户分析数据已记录: %s\n", user["name"])
    })
    
    // 注册订单相关事件处理器
    bus.Subscribe("order.placed", func(event Event) {
        order := event.Data.(map[string]interface{})
        fmt.Printf("新订单: %s (金额: %.2f)\n", order["id"], order["amount"])
    })
    
    bus.Subscribe("order.placed", func(event Event) {
        fmt.Println("库存已更新")
    })
    
    // 应用中间件
    bus.Use("user.created", LoggingMiddleware)
    bus.Use("user.created", ErrorHandlingMiddleware)
    bus.Use("order.placed", LoggingMiddleware)
    
    // 发布事件
    fmt.Println("=== 发布用户创建事件 ===")
    bus.Publish("user.created", map[string]interface{}{
        "name":  "张三",
        "email": "zhangsan@example.com",
    })
    
    time.Sleep(200 * time.Millisecond)
    
    fmt.Println("\n=== 发布订单事件 ===")
    bus.Publish("order.placed", map[string]interface{}{
        "id":     "ORDER-001",
        "amount": 99.99,
    })
    
    time.Sleep(200 * time.Millisecond)
}

🎓 Summary

In this chapter, we explored advanced features of Go functions in depth:

  • Anonymous Functions: Flexible function definition and usage
  • Closures: Functions that capture variables from outer scopes
  • Variadic Parameters: Handling an indefinite number of arguments
  • Higher-Order Functions: Functions as parameters and return values
  • Function Composition: Building complex processing pipelines
  • Recursion Optimization: Better approaches to recursive implementations
  • Practical Applications: Real-world examples such as event handling systems

Mastering these advanced function features will help you write more flexible, reusable, and elegant Go code.


Next, we will learn about Go Language Variable Scope to understand variable lifetimes and visibility rules.

::: tip Function Design Recommendations

  • Prefer pure functions (no side effects)
  • Use closures judiciously; avoid excessive reliance on external state
  • Choose the most appropriate implementation between recursion and iteration
  • Leverage higher-order functions to improve code reusability :::