#"declared but not used" I clearly used both?

8 messages · Page 1 of 1 (latest)

gray rampart
#
package main

import (
    "fmt"
    "github.com/gocolly/colly"
    "log"
)

func url(c *colly.Collector) {

    for count := 0; count < 110; count++ {
        url := fmt.Sprintf("https://www.ebay.com/sch/i.html?_from=R40&_nkw=iPhone&_sacat=0&_pgn=%d&rt=nc", count)
        fmt.Println(url)
        err := c.Visit(url)

        if err != nil {
            log.Println(err)
        }
    }

}

func main() {

    var titleArray []string
    var priceArray []string

    c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (X11; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0"))

    c.OnHTML(".s-item__link", func(element *colly.HTMLElement) {
        element.ChildText("s-item__title")
        titleArray := append(titleArray, element.Text)

        fmt.Println(element.Text)
    })

    c.OnHTML(".s-item__detail.s-item__detail--primary", func(element *colly.HTMLElement) {
        element.ChildText(".s-item__price")
        priceArray := append(priceArray, element.Text)

        fmt.Println(element.Text)
    })

    url(c)

    defer func() {
        fmt.Println(len(titleArray), len(priceArray))
        for i := 0; i < len(titleArray); i++ {
            fmt.Printf("Title %s | Price %s \n", titleArray[i], priceArray[i])
        }
    }()
}

How am I getting this error when I clearly used both?

shut fern
#

What line?

#

ah your titleArray and priceArray.

The reason for that is you are using := inside your closures... that should be =.

#

:= declares a new variable

#

= assigns to the existing variables

gray rampart
#

are you talking about append?

#

OH