re

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

View Source
var MaxAttempts = 10

Functions

func Do

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

import (
	"fmt"
	"simonwaldherr.de/go/golibs/re"
	"time"
)

func main() {
	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
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"simonwaldherr.de/go/golibs/re"
)

func main() {
	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
	})
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL