Last active
October 14, 2025 11:36
-
-
Save JoeGlines/a0c237891834f4f685bf95cd71a8bfa5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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