Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: x/net/html: add Node.All() iterator #62113

Open
earthboundkid opened this issue Aug 17, 2023 · 1 comment
Open

proposal: x/net/html: add Node.All() iterator #62113

earthboundkid opened this issue Aug 17, 2023 · 1 comment
Labels
Milestone

Comments

@earthboundkid
Copy link
Contributor

earthboundkid commented Aug 17, 2023

This is assuming #61405 is accepted.

Iterating through all the children of an html.Node is tedious. The current code has this example of a recursive function printing out links:

var f func(*html.Node)
f = func(n *html.Node) {
	if n.Type == html.ElementNode && n.Data == "a" {
		for _, a := range n.Attr {
			if a.Key == "href" {
				fmt.Println(a.Val)
				break
			}
		}
	}
	for c := n.FirstChild; c != nil; c = c.NextSibling {
		f(c)
	}
}
f(doc)

It would be much nicer with an iterator:

for n := range doc.All() {
	if n.Type == html.ElementNode && n.Data == "a" {
		for _, a := range n.Attr {
			if a.Key == "href" {
				fmt.Println(a.Val)
				break
			}
		}
	}
}
@gopherbot gopherbot added this to the Proposal milestone Aug 17, 2023
@ianlancetaylor
Copy link
Contributor

CC @neild

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

3 participants