avast/retry-go

Retry a struct function

alok87 opened this issue · 2 comments

type Getter struct {
}

func (g *Getter) Get(url string) error {
	response, err := http.Get(url)
    if err != nil {
		fmt.Println("error")
		return err
    }
    defer response.Body.Close()

	if response.StatusCode != 200 {
		fmt.Println("error ! 200")
		return fmt.Errorf("not 200 got, response: %v\n", response)
	}

	fmt.Println("success")
	return nil
}

I want to retry Get(url string).

Please suggest if it is possible using this library.

JaSei commented

Hi @alok87

look at this snippet, it's what you want?

g := Getter{}
retry.Do(func() error {
    return g.Get(url)
})

yes this looks awesome, will move to this.