Advertisement
cwchen

[Go] Using lists.

Nov 21st, 2017
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.56 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "errors"
  5.     "github.com/cwchentw/algo-golang/list"
  6.     "log"
  7. )
  8.  
  9. func main() {
  10.     l := list.New()
  11.  
  12.     for i := 1; i <= 10; i++ {
  13.         l.Push(i)
  14.     }
  15.  
  16.     evens, _ := l.Select(isEven)
  17.     for e := range evens.Iter() {
  18.         n, _ := e.(int)
  19.         if n % 2 != 0 {
  20.             log.Fatalln("Error Select result")
  21.         }
  22.     }
  23. }
  24.  
  25. func isEven(a interface{}) (bool, error) {
  26.     n, ok := a.(int)
  27.     if ok != true {
  28.         return false, errors.New("Failed type assertion on a")
  29.     }
  30.  
  31.     return n % 2 == 0, nil
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement