Skip to content

Instantly share code, notes, and snippets.

@jbowles
Last active December 23, 2015 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbowles/6603728 to your computer and use it in GitHub Desktop.
Save jbowles/6603728 to your computer and use it in GitHub Desktop.
Playing with function passing in golang
package main
import "fmt"
type HelloList struct {
people []string
}
func NewHelloList(names []string) *HelloList {
return &HelloList{people: names}
}
func sayHelloOne(name string) {
fmt.Println("Hello", name)
}
func (h *HelloList) MapOne(f func(string)) {
for _, value := range h.people {
f(value)
}
}
func sayHelloTwo(name string) (hello string) {
hello = "hello " + name
return
}
func (h *HelloList) MapTwo(f func(string) string) {
for _, value := range h.people {
fmt.Println("returns string and we print it here", f(value))
}
}
func ScreenOne(people []string) func(string) bool {
return func(name string) bool {
for _, nv := range people {
if nv == name {
return true
}
}
return false
}
}
func ScreenTwo(people []string, screen_type int) (f func(string) bool) {
if screen_type < 2 {
f = func(name string) bool {
for _, nv := range people {
if nv == name {
return true
}
}
return false
}
} else if screen_type > 2 {
f = func(name string) bool {
for _, nv := range people {
if nv == name {
return false
}
}
return true
}
}
return
}
func main() {
name_list1 := []string{"jon", "meg", "sally"}
l1 := NewHelloList(name_list1)
name_list2 := []string{"ibraham", "lev", "ariana"}
l2 := NewHelloList(name_list2)
// sayHelloOne returns println
sayHelloOne("yo... ")
// function with closure that returns nothing
l1.MapOne(sayHelloOne)
// sayHelloTwo returns string, set to var, print
t := sayHelloTwo(" ...dude!")
fmt.Println("sayHelloTwo reutrns a string and we have to print it out:", t)
// function with closure that returns string
l2.MapTwo(sayHelloTwo)
// slice of strings
screened_peeps := []string{name_list1[0], "bob", name_list2[2]}
// variable set from Screen (which returns a function that accepts string parameter)
var screener = ScreenOne(screened_peeps)
bob := screener("bob")
jon := screener("jon")
meg := screener("meg")
ariana := screener("ariana")
fmt.Printf("Screening One Results: bob = %v, jon = %v, meg = %v, ariana = %v\n", bob, jon, meg, ariana)
s_type := 3
var screener_two = ScreenTwo(screened_peeps, s_type)
bob2 := screener_two("bob")
jon2 := screener_two("jon")
meg2 := screener_two("meg")
ariana2 := screener_two("ariana")
fmt.Printf("Screening Two Results: bob = %v, jon = %v, meg = %v, ariana = %v\n", bob2, jon2, meg2, ariana2)
}
/*
* output ==
Hello yo...
Hello jon
Hello meg
Hello sally
sayHelloTwo reutrns a string and we have to print it out: hello ...dude!
returns string and we print it here hello ibraham
returns string and we print it here hello lev
returns string and we print it here hello ariana
Screening One Results: bob = true, jon = true, meg = false, ariana = true
Screening Two Results: bob = false, jon = false, meg = true, ariana = false
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment