Created
March 23, 2025 03:38
-
-
Save gigenthomas/577f652fcd3ad5a64bea87a65193b20c 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" | |
| // Define a function type called 'mathOperation' | |
| type mathOperation func(int, int) int | |
| // Define some functions that match the 'mathOperation' type | |
| func add(a, b int) int { | |
| return a + b | |
| } | |
| func multiply(a, b int) int { | |
| return a * b | |
| } | |
| // Function that takes a 'mathOperation' as an argument | |
| func calculate(a, b int, operation mathOperation) int { | |
| return operation(a, b) | |
| } | |
| func main() { | |
| // Using the function type to call 'add' or 'multiply' | |
| fmt.Println(calculate(3, 4, add)) // Outputs: 7 | |
| fmt.Println(calculate(3, 4, multiply)) // Outputs: 12 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment