Last active
November 4, 2018 04:14
-
-
Save wuriyanto48/ed88f17d4685394407e6fbef61134a97 to your computer and use it in GitHub Desktop.
Golang Function as Parameter example
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
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
type fo func(key string, value string) bool | |
func Check(key string, data []string, op fo) []string{ | |
var result []string | |
for _, v := range data { | |
if op(key, v){ | |
result = append(result, v) | |
} | |
} | |
return result | |
} | |
type Persons []string | |
func main() { | |
persons := Persons{"Andi", "Beni", "Wuri", "Alan", "Ana"} | |
res := Check("an", persons, func(k, s string) bool { | |
return strings.Contains(strings.ToLower(s), strings.ToLower(k)) | |
}) | |
fmt.Println(res) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment