Skip to content

Instantly share code, notes, and snippets.

@JoeGlines
Last active October 14, 2025 11:36
Show Gist options
  • Save JoeGlines/a0c237891834f4f685bf95cd71a8bfa5 to your computer and use it in GitHub Desktop.
Save JoeGlines/a0c237891834f4f685bf95cd71a8bfa5 to your computer and use it in GitHub Desktop.
package main
import "fmt"
// Regular function
func greetUser(name string) string {
return "Hello, " + name
}
// Function that takes a function as parameter
func applyOperation(x int, y int, op func(int, int) int) int {
return op(x, y)
}
// Anonymous function (lambda)
func main() {
message := greetUser("Alice")
fmt.Println(message)
// Pass a function as an argument
add := func(a int, b int) int {
return a + b
}
result := applyOperation(5, 3, add)
fmt.Println(result) // 8
// Inline anonymous function
result2 := applyOperation(5, 3, func(a int, b int) int {
return a * b
})
fmt.Println(result2) // 15
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment