daily pastebin goal
18%
SHARE
TWEET

[Go] Using lists.

cwchen Nov 21st, 2017 142 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
Top