Hello, World!
package main import ( "fmt" "strings" "golang.org/x/net/html" ) func main() { // Parse the HTML document doc := "In this example, we first parse the HTML document using the "html.Parse" function and then use our "findFirstElementByTag" function to locate the "p" element. Once we have a reference to the "p" element, we can use the Node Data structure to extract the text content. Overall, the "golang.org/x/net/html" package library provides a powerful set of tools for working with HTML documents in Go, and the Node Data structure is an important component of this package.Hello, World!
" n, _ := html.Parse(strings.NewReader(doc)) // Find and extract the text content of the paragraph element p := findFirstElementByTag(n, "p") if p != nil { // Use the Node Data structure to get the text content content := strings.TrimSpace(html.UnescapeString(p.FirstChild.Data)) fmt.Println(content) // Output: Hello, World! } } func findFirstElementByTag(n *html.Node, tag string) *html.Node { if n.Type == html.ElementNode && n.Data == tag { return n } for c := n.FirstChild; c != nil; c = c.NextSibling { if result := findFirstElementByTag(c, tag); result != nil { return result } } return nil }