Tuesday, September 30, 2025
HomeGolangColly callback not receiving variable from go func - Getting Assist

Colly callback not receiving variable from go func – Getting Assist


Principally, it’s like this:

x := "It labored earlyer lol"
go func(x string){
  c.onHTML(...){
    print(x)
  }
  c.go to(https://www.website.com)
}

in order that’s the gist once I print the variables which might be clean however exterior of the go func they’re outlined right here’s the complete mum or dad operate:

eventCollector.OnHTML(".rgMasterTable tr", func(h *colly.HTMLElement) {
    eventName := h.ChildText("td:nth-child(3) a")
    eventURL := h.ChildAttr("td:nth-child(3) a", "href")
    state := h.ChildText("td:nth-child(2)")
    wgFR.Add(1)             // Increment WaitGroup counter for every goroutine
    semaphore <- struct{}{} // Purchase a token

    go func(eventName, eventURL, state string) {
       defer wgFR.Executed() // Sign completion when the goroutine exits
       defer func() { <-semaphore }()
       contestCollector := eventCollector.Clone()
       var postedDateStr string
       contestCollector.OnHTML("#ctl00_ContentPlaceHolder1_FormView1_Report_2Label", func(d *colly.HTMLElement) {
          postedDateStr = d.Textual content
       })

       contestCollector.OnHTML(".rgMasterTable tr", func(c *colly.HTMLElement) { // troubled line

          contestName := c.ChildText("td:nth-child(1)")
          contestURL := c.ChildAttr("td:nth-child(3) a", "href")
          if contestURL == "" {
             contestURL = "FORMAT-ERROR"
          } // tmp handler for doc type outcomes
          postedDate, timeErr := time.Parse("Jan 2, 2006", postedDateStr)
          if timeErr != nil {
             log.Printf("Error parsing time from %s", eventURL)
          }
          contest := Contest{
             EventName:   eventName,
             ContestName: contestName,
             PostedDate:  postedDate,
             ContestURL:  contestURL,
             State:       state,
             Current:     true,
          }
          fResults = append(fResults, contest)
       })
       err := contestCollector.Go to("https://www.judgingcard.com/Outcomes/" + eventURL)
       if err != nil {
          log.Printf("Couldn't discover occasion: %s -- %s", eventURL, eventName)
       }
       contestCollector.Wait() // Anticipate the internal collector to complete
    }(eventName, eventURL, state)
})

within the full model all variables handed to the go func return clean if within the callback (contestCollector.OnHTML) sadly I’m undecided climate concern likes within the go routine, the truth that is as callback or no matter else.
Thanks prematurely!

This appears fairly convoluted. It appears like you’re utilizing github.com/gocolly/colly. Why is that part in a goroutine precisely? I might first strive eliminating that. It appears as if the collectors are already utilizing goroutines (I’m simply guessing since they’ve a Wait operate; I didn’t have a look at the docs to verify this).

This code appears prefer it’s lacking some stuff (like the place are you ready for wgFR? The place is wgFR even outlined?) so off the highest of my head one one thing might be modifying the strings in your outer features that you just won’t be anticipating. Within the problematic contestCollector.OnHTML callback you’re not passing these values to that operate so it might be falling prey to one thing like this:

func most important() {
	myAwesomeValue := "superior"
	wg := sync.WaitGroup{}
	wg.Add(1)
	go func() {
		time.Sleep(time.Millisecond)
		fmt.Println("The worth is", myAwesomeValue)
		wg.Executed()
	}()
	myAwesomeValue = "not superior"
	wg.Wait()
}

… which prints The worth will not be superior.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments