Implement Single and SingleMatcher

This commit is contained in:
Martin Angers
2021-06-13 14:34:32 -04:00
parent e1f2d60505
commit 0126a1fd88
2 changed files with 76 additions and 1 deletions

View File

@@ -80,3 +80,31 @@ func ExampleNewDocumentFromReader_string() {
// Output: Header
}
func ExampleSingle() {
html := `
<html>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
</body>
</html>
`
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
log.Fatal(err)
}
// By default, the selector string selects all matching nodes
multiSel := doc.Find("div")
fmt.Println(multiSel.Text())
// Using goquery.Single, only the first match is selected
singleSel := doc.FindMatcher(goquery.Single("div"))
fmt.Println(singleSel.Text())
// Output:
// 123
// 1
}