优化ErrGroup结果切片的定义
This commit is contained in:
parent
e57918df05
commit
9fce7acead
@ -47,13 +47,13 @@ func GetAllCityWeatherData(timeOut time.Duration) ([]*WeatherData, error) {
|
|||||||
// 创建一个携带cancel的context
|
// 创建一个携带cancel的context
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
|
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
|
||||||
defer cancel() // 确保所有路径都调用cancel
|
defer cancel() // 确保所有路径都调用cancel
|
||||||
list := make([]*WeatherData, 0)
|
|
||||||
|
|
||||||
// 初始化一个ErrGroup对象
|
// 初始化一个ErrGroup对象
|
||||||
g, ctx := errgroup.WithContext(ctx)
|
g, ctx := errgroup.WithContext(ctx)
|
||||||
|
|
||||||
// 城市列表
|
// 城市列表
|
||||||
cities := []string{"New York", "Tokyo", "Berlin", "Paris", "Beijing"}
|
cities := []string{"New York", "Tokyo", "Berlin", "Paris", "Beijing"}
|
||||||
|
list := make([]*WeatherData, 0, len(cities))
|
||||||
|
|
||||||
// 循环启动goroutine来获取每个城市的天气数据
|
// 循环启动goroutine来获取每个城市的天气数据
|
||||||
for _, city := range cities {
|
for _, city := range cities {
|
||||||
|
Loading…
Reference in New Issue
Block a user