From: Nigel Tao Date: Wed, 17 Oct 2012 23:25:50 +0000 (+1100) Subject: exp/html: update package docs and add an example; a node's children is X-Git-Tag: go1.1rc2~2108 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9dc3152668319fce81ec72055051cee47d4c801d;p=gostls13.git exp/html: update package docs and add an example; a node's children is a linked list, not a slice. R=r, minux.ma CC=golang-dev https://golang.org/cl/6618055 --- diff --git a/src/pkg/exp/html/doc.go b/src/pkg/exp/html/doc.go index 56b194ffb9..4dd453091c 100644 --- a/src/pkg/exp/html/doc.go +++ b/src/pkg/exp/html/doc.go @@ -84,7 +84,7 @@ example, to process each anchor node in depth-first order: if n.Type == html.ElementNode && n.Data == "a" { // Do something with n... } - for _, c := range n.Child { + for c := n.FirstChild; c != nil; c = c.NextSibling { f(c) } } diff --git a/src/pkg/exp/html/example_test.go b/src/pkg/exp/html/example_test.go new file mode 100644 index 0000000000..c15e9a2bd8 --- /dev/null +++ b/src/pkg/exp/html/example_test.go @@ -0,0 +1,39 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This example demonstrates parsing HTML data and walking the resulting tree. +package html_test + +import ( + "exp/html" + "fmt" + "log" + "strings" +) + +func ExampleParse() { + s := `

Links:

` + doc, err := html.Parse(strings.NewReader(s)) + if err != nil { + log.Fatal(err) + } + 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) + // Output: + // foo + // /bar/baz +}