Last active
August 29, 2015 13:58
-
-
Save TripleDogDare/10407694 to your computer and use it in GitHub Desktop.
Passing functions as parameters
This file contains 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
// http://play.golang.org/p/kmXecwWIMR | |
package main | |
import "fmt" | |
/* | |
Output: | |
Hello, playground | |
A: 1 | |
A: 2 | |
A: 3 | |
A: 4 | |
A: 5 | |
B: 6 | |
B: 7 | |
B: 8 | |
B: 9 | |
A: 0 | |
*/ | |
func main() { | |
fmt.Println("Hello, playground") | |
GreaterThan([]int{1,2,3,4,5,6,7,8,9,0},5,A,B) | |
} | |
func GreaterThan(slice []int, x int, less func(int), more func(int)) { | |
for _, v := range slice { | |
if v > x { | |
more(v) | |
} else { | |
less(v) | |
} | |
} | |
} | |
func A(i int) { | |
fmt.Println("A:", i) | |
} | |
func B(i int) { | |
fmt.Println("B:", i) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment