diff --git a/err_group/weather.go b/err_group/weather.go index 1ebe5d2..6793db1 100644 --- a/err_group/weather.go +++ b/err_group/weather.go @@ -47,13 +47,13 @@ func GetAllCityWeatherData(timeOut time.Duration) ([]*WeatherData, error) { // 创建一个携带cancel的context ctx, cancel := context.WithTimeout(context.Background(), timeOut) defer cancel() // 确保所有路径都调用cancel - list := make([]*WeatherData, 0) // 初始化一个ErrGroup对象 g, ctx := errgroup.WithContext(ctx) // 城市列表 cities := []string{"New York", "Tokyo", "Berlin", "Paris", "Beijing"} + list := make([]*WeatherData, 0, len(cities)) // 循环启动goroutine来获取每个城市的天气数据 for _, city := range cities {