Skip to content

Instantly share code, notes, and snippets.

@gigenthomas
Created March 23, 2025 03:38
Show Gist options
  • Select an option

  • Save gigenthomas/577f652fcd3ad5a64bea87a65193b20c to your computer and use it in GitHub Desktop.

Select an option

Save gigenthomas/577f652fcd3ad5a64bea87a65193b20c to your computer and use it in GitHub Desktop.
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