Last active
September 7, 2020 17:11
-
-
Save thomd/40dc818836fe1eef0cc37a587cc862db to your computer and use it in GitHub Desktop.
Go Examples #golang #go #code
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 model | |
import ( | |
"fmt" | |
) | |
type Person struct { | |
Name string | |
Age int | |
} | |
func (p *Person) String() string { | |
return fmt.Sprintf("%v (%v)", p.Name, p.Age) | |
} | |
type PersonToBool func(*Person) bool | |
type PersonList []*Person | |
func (pl PersonList) Filter(f PersonToBool) PersonList { | |
var ret []*Person | |
for _, p := range pl { | |
if f(p) { | |
ret = append(ret, p) | |
} | |
} | |
return ret | |
} | |
// USAGE | |
// | |
// var pl model.PersonList | |
// pl = append(pl, &model.Person{Name: "Jane", Age: 32}) | |
// pl = append(pl, &model.Person{Name: "Joe", Age: 27}) | |
// pl2 := pl.Filter(func(p *model.Person) bool { | |
// return p.Age > 30 | |
// }) | |
// for _, p := range pl2 { | |
// fmt.Println(p) | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment