doc, _ := html.Parse(strings.NewReader("Click me")) a := doc.FirstChild.FirstChild.FirstChild href := "" for _, attr := range a.Attr { if attr.Key == "href" { href = attr.Val break } } fmt.Println(href)
doc, _ := html.Parse(strings.NewReader("")) img := doc.FirstChild.FirstChild.FirstChild for i, attr := range img.Attr { if attr.Key == "src" { img.Attr[i].Val = "bar.jpg" } } buf := new(bytes.Buffer) html.Render(buf, doc) fmt.Println(buf.String())In this example, we again parse an HTML document represented as a string. We navigate to the first image element and loop over its attributes, looking for the "src" attribute. Once we find it, we modify its value to "bar.jpg". Finally, we use the html.Render function to render the modified document to a string and print it. Overall, the golang.org.x.net.html package library provides a powerful set of tools for working with HTML documents in Go, including support for parsing, querying, and modifying both document structure and element attributes.