老师你好,这种是什么写法? <- rateLimiter, 问题是,rateLimiter不是chan哦

来源:16-4 队列实现调度器

qq_自_3

2021-02-16

var rateLimiter = time.Tick(100*time.Millisecond)
func Fetch(url string) ([]byte, error) {
	<-rateLimiter
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
	}
	return ioutil.ReadAll(resp.Body)
}


我知道了,原来返回值是chan
time.Tick(100*time.Millisecond) <-chan

写回答

1回答

ccmouse

2021-02-21

是这样的。chan是一等公民,不少函数都会返回chan。我们自己也可以采用这样的做法,让函数返回chan 

0
0

Google资深工程师深度讲解Go语言 由浅入深掌握Go语言

语法+分布式爬虫实战 为转型工程师量身打造

5995 学习 · 1909 问题

查看课程