79499716

Date: 2025-03-11 05:27:53
Score: 0.5
Natty:
Report link

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)
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Cathy Wood