Home > database >  golang function returns empty slice when slice length more than 700 item
golang function returns empty slice when slice length more than 700 item

Time:10-09

I have a function calls API to get items and return items to sync with another db. I can specify limit of items that returns. If the limit is 700 it return 700 item. if more than 700 it return empty slice.

func (d *dataBaseWriterImpl) WriteProducts() error {
    username := os.Getenv("USERNAME")
    password := os.Getenv("PASSWORD")

    // get token
    token, err := d.authService.Login(username, password)
    if err != nil {
        return err
    }

    // load products
    // here the problem if count more than 700 <----
    products, _ := d.scrapperService.LoadProducts(702, 1, token)
    log.Printf("products count -> %d", len(products)) // gives me 0 <-----

    errChan := make(chan error, 2)
    var cats []models.Category
    db.Conn.Model(&models.Category{}).Find(&cats)
    var catIds []string
    for _, cat := range cats {
        catIds = append(catIds, cat.ExternalId)
    }

    for _, prod := range products {
        if d.isValidProduct(catIds, prod.PrimaryVariant.CategoryID) {
            log.Printf("update id -> %v", prod.PrimaryVariant.ID)
            go d.saveOrUpdateProducts(prod.PrimaryVariant, errChan)
        } else {
            log.Printf("will not update id -> %v", prod.PrimaryVariant.ID)
            go func() {
                errChan <- nil
            }()
        }

    }
    for range products {
        err := <-errChan
        if err != nil {
            return err
        }
    }
    return nil
}

I log items length before return it return the actual value in the LoadProducts implementation.

func (s serviceImpl) LoadProducts(count uint, page uint, token string) ([]models.VariantsResult, error) {

    jsonBody := []byte(fmt.Sprintf(`{"pageSize": %d, "page": %d, "sortBy": "updatedAt"}`, count, page))
    body := bytes.NewBuffer(jsonBody)

    // Create request
    req, err := http.NewRequest("POST", fmt.Sprintf("%v/api/variantGroups/search", os.Getenv("BACKEND")), body)
    if err != nil {
        log.Print(err)
    }
    // Headers
    //req.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:84.0) Gecko/20100101 Firefox/84.0")
    req.Header.Add("Accept", "application/json, text/plain, */*")
    req.Header.Add("Authorization", fmt.Sprintf("Bearer %v", token))
    req.Header.Add("Content-Type", "application/json")

    // Fetch Request
    resp, err := s.client.Do(req)

    if err != nil {
        fmt.Println("Failure : ", err)
    }

    //[]models.Product `json:"data"`
    // Read Response
    type Response struct {
        Results []models.VariantsResult `json:"results"`
        Count   int                     `json:"count"`
    }
    var response Response

    err = json.NewDecoder(resp.Body).Decode(&response)
    log.Printf("products -> %d", len(response.Results)) // gives me actual number <--
    if err != nil {
        return nil, err
    }
    return response.Results, nil
}

CodePudding user response:

Almost certainly you are returning an error, but then ignoring it.

A nil slice has a length and capacity of 0 and has no underlying array. https://tour.golang.org/moretypes/12

You return nil, err here:

// in LoadProducts
if err != nil {
    return nil, err
}

but then do

products, _ := d.scrapperService.LoadProducts(702, 1, token)
log.Printf("products count -> %d", len(products)) // gives me 0 <---

which should be

products, err := d.scrapperService.LoadProducts(702, 1, token)
if err != nil {
    panic(err) // dont actually panic, but figure out what went wrong
}
log.Printf("products count -> %d", len(products)) // gives me 0 <---
  •  Tags:  
  • go
  • Related