how write link in links

43 views Asked by At
package main

import (
    "fmt"
    "github.com/PuerkitoBio/goquery"
    "log"
)

func main() {

    var links string = ""

    doc, err := goquery.NewDocument("https://dev.bolshoi.ru/visit/buyingnew/")
    if err != nil {
        log.Fatal(err)
    }

    doc.Find("a:contains(\"График предварительной продажи билетов на декабрь 2023\")").Each(func(i int, s *goquery.Selection) {
        link, _ := s.Attr("href")
        fmt.Printf(link)
    })
}

I just started learning the language, I don't understand well what needs to be done

1

There are 1 answers

0
maxm On BEST ANSWER

Here is how you assign the link value to the outer links variable:

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/PuerkitoBio/goquery"
)

func main() {
    var links string = ""

    resp, err := http.Get("https://dev.bolshoi.ru/visit/buyingnew/")
    if err != nil {
        log.Fatal(err)
    }
    doc, err := goquery.NewDocumentFromReader(resp.Body)
    if err != nil {
        log.Fatal(err)
    }

    doc.Find("a:contains(\"График предварительной продажи билетов на декабрь 2023\")").Each(func(i int, s *goquery.Selection) {
        link, _ := s.Attr("href")
        fmt.Println(link)
        links = link
    })
    fmt.Println(links)
}

If there are multiple links you could make a slice:

links := []string{}

//...

doc.Find("a:contains(\"График предварительной продажи билетов на декабрь 2023\")").Each(func(i int, s *goquery.Selection) {
    link, _ := s.Attr("href")
    fmt.Println(link)
    links = append(links, link)
})
fmt.Println(links)