Use the reflect package to reduce this type of duplication.
// GetJsons decodes the JSON responses to the slice pointed
// to by target. The target argument must be a pointer to
// a slice of cheese.
func GetJsons(urls []string, target any) []error {
errors := make([]error, len(urls))
v := reflect.ValueOf(target).Elem()
v.Set(reflect.MakeSlice(v.Type(), len(urls), len(urls)))
var wg sync.WaitGroup
wg.Add(len(urls))
for i, url := range urls {
go func() {
defer wg.Done()
errors[i] = GetJson(url, v.Index(i).Addr().Interface())
}()
}
return errors
}
Replace calls to GetJsonAs*Multiple with calls to GetJsons:
var hashmaps []Map
errors := GetJsons(urls, &hashmaps)