DEV Community

Cover image for Understanding First-Order Functions in Go
Md Abu Musa
Md Abu Musa

Posted on

1 1

Understanding First-Order Functions in Go

What is a First-Order Function?

A first-order function is a function that does not take another function as an argument and does not return a function. It operates only on standard data types such as integers, strings, structs, arrays, etc.

In contrast, higher-order functions either take a function as an argument or return a function.


Characteristics of First-Order Functions

  1. Takes and returns basic data types (int, string, struct, slice, etc.).
  2. No function parameters (does not accept another function as an argument).
  3. No function return values (does not return another function).
  4. Easier to understand and use compared to higher-order functions.

Examples of First-Order Functions in Go

1️⃣ Simple Function with Primitive Data Types

This function takes two integers as input and returns their sum.

package main

import "fmt"

func add(a int, b int) int {
    return a + b
}

func main() {
    result := add(5, 3)
    fmt.Println("Sum:", result) // Output: Sum: 8
}
Enter fullscreen mode Exit fullscreen mode

Why First-Order?

  • add operates only on integer values.
  • It neither accepts nor returns another function.

2️⃣ First-Order Function Using a Struct

A function that processes a struct (custom data type).

package main

import "fmt"

type User struct {
    Name  string
    Age   int
}

// Function that operates on a struct and returns a string
func greet(user User) string {
    return "Hello, " + user.Name + "!"
}

func main() {
    u := User{Name: "Alice", Age: 25}
    message := greet(u)
    fmt.Println(message) // Output: Hello, Alice!
}
Enter fullscreen mode Exit fullscreen mode

Why First-Order?

  • greet accepts a User struct and returns a string.
  • It does not accept or return functions.

3️⃣ First-Order Function with Slices

A function that processes an array or slice.

package main

import "fmt"

func sumArray(numbers []int) int {
    sum := 0
    for _, num := range numbers {
        sum += num
    }
    return sum
}

func main() {
    nums := []int{1, 2, 3, 4, 5}
    fmt.Println("Total Sum:", sumArray(nums)) // Output: 15
}
Enter fullscreen mode Exit fullscreen mode

Why First-Order?

  • sumArray processes a slice of integers.
  • No function parameters or return values are involved.

How First-Order Functions Differ from Higher-Order Functions?

Feature First-Order Function Higher-Order Function
Accepts Function as Parameter? ❌ No ✅ Yes
Returns Another Function? ❌ No ✅ Yes
Example sumArray([]int) operate(4, 2, func(x, y int) int { return x * y })

When to Use First-Order Functions?

✅ When working with simple computations that don’t need function references.

✅ When modularity and readability are more important than flexibility.

✅ When performing basic operations like calculations, struct processing, and data manipulation.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (1)

Collapse
 
consolvex profile image
Consolex

This is the first time I see something like this, there is nothing like this in the lesson)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more