I want to get a document tree. Then, first, I displayed all elements name. But my code run loop. How can I do?
package main
import (
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
func getTagName(s *goquery.Selection) {
for _, n := range s.Nodes {
if n.Type != html.ElementNode {
continue
}
println(n.Data)
getTagName(s.Children())
}
}
func main() {
doc, _ := goquery.NewDocument("https://news.ycombinator.com/")
doc.Find("html body").Each(func(_ int, s *goquery.Selection) {
getTagName(s)
})
}
It seems to work with this:
package main
import (
"os"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
var areWeLooping = make(map[*goquery.Selection]struct{})
func getTagName(s *goquery.Selection) {
if _, weAreLooping := areWeLooping[s]; weAreLooping {
println("loop detected")
os.Exit(1)
}
areWeLooping[s] = struct{}{}
for _, n := range s.Nodes {
if n.Type != html.ElementNode {
continue
}
println(n.Data)
}
s.Children().Each(func(_ int, s *goquery.Selection) {
getTagName(s)
})
}
func main() {
doc, _ := goquery.NewDocument("https://news.ycombinator.com/")
doc.Find("html body").Children().Each(func(_ int, s *goquery.Selection) {
getTagName(s)
})
}
Having getTagName(s.Children())
inside the loop was causing trouble.