Skip to content

Commit 7757e27

Browse files
authored
add examples for NewDocumentFromReader (#254)
1 parent ea1bc64 commit 7757e27

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

example_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"log"
66
"net/http"
7+
"os"
8+
"strings"
79

810
"github.com/PuerkitoBio/goquery"
911
)
@@ -39,3 +41,42 @@ func Example() {
3941

4042
// xOutput: voluntarily fail the Example output.
4143
}
44+
45+
// This example shows how to use NewDocumentFromReader from a file.
46+
func ExampleNewDocumentFromReader_file() {
47+
// create from a file
48+
f, err := os.Open("some/file.html")
49+
if err != nil {
50+
log.Fatal(err)
51+
}
52+
defer f.Close()
53+
doc, err := goquery.NewDocumentFromReader(f)
54+
if err != nil {
55+
log.Fatal(err)
56+
}
57+
// use the goquery document...
58+
_ = doc.Find("h1")
59+
}
60+
61+
// This example shows how to use NewDocumentFromReader from a string.
62+
func ExampleNewDocumentFromReader_string() {
63+
// create from a string
64+
data := `
65+
<html>
66+
<head>
67+
<title>My document</title>
68+
</head>
69+
<body>
70+
<h1>Header</h1>
71+
</body>
72+
</html>`
73+
74+
doc, err := goquery.NewDocumentFromReader(strings.NewReader(data))
75+
if err != nil {
76+
log.Fatal(err)
77+
}
78+
header := doc.Find("h1").Text()
79+
fmt.Println(header)
80+
81+
// Output: Header
82+
}

0 commit comments

Comments
 (0)