...

Package re

import "simonwaldherr.de/go/golibs/re"
Overview
Index
Examples

Overview ▾

wanna do something several times? Then this is the correct package for you.

Variables

var MaxAttempts = 10

func Do

func Do(wait time.Duration, fn func(chan<- interface{})) (<-chan interface{}, chan<- bool)

Example

Code:

values := []string{"a", "b", "c", "d", "e"}
data, stop := re.Do(time.Millisecond*100, func(data chan<- interface{}) {
    for i := 0; i < 5; i++ {
        data <- fmt.Sprintf("%v", values[i])
    }
})
j := 0
for i := 0; i < 5; i++ {
    select {
    case x := <-data:
        fmt.Printf("%v:%v\n", x, i)
        j++
    }
}
stop <- true
time.Sleep(time.Millisecond * 500)

Output:

a:0
b:1
c:2
d:3
e:4

func Try

func Try(retrys int, fn func() (err error)) error

Example

Code:

re.Try(5, func() error {
    var err error
    response, err := http.Get("http://golang.org/")
    if err != nil {
        fmt.Printf("%s", err)
        return err
    } else {
        defer response.Body.Close()
        contents, err := ioutil.ReadAll(response.Body)
        if err != nil {
            fmt.Printf("%s", err)
            return err
        }
        fmt.Printf("%s\n", string(contents))
    }
    return err
})