ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
>[info] retry-go(重试机制) CSDN文档:https://blog.csdn.net/jiaoyangwm/article/details/136500332 * **安装:** ~~~ go get "github.com/avast/retry-go" ~~~ * **示例:** ~~~ package main import ( "fmt" "github.com/avast/retry-go" "io" "net/http" ) func main() { urls := []string{ "http://example.com", // url 真实存在 "http://not-exist.com", // url 不存在 } for _, url := range urls { f(url) } } func f(url string) { fmt.Println("开始处理: ", url) var body []byte err := retry.Do(func() error { resp, err := http.Get(url) if err != nil { return err } defer resp.Body.Close() body, err = io.ReadAll(resp.Body) if err != nil { return err } return nil }, retry.Attempts(3)) if err != nil { panic(err) } _ = body fmt.Println("处理成功") } ~~~ * **返回:** ``` 开始处理: http://example.com 处理成功 开始处理: http://not-exist.com panic: All attempts fail: #1: Get "http://not-exist.com": dial tcp: lookup not-exist.com: no such host #2: Get "http://not-exist.com": dial tcp: lookup not-exist.com: no such host #3: Get "http://not-exist.com": dial tcp: lookup not-exist.com: no such host ```