4
goquery Get Number of Children Nodes
source link: http://siongui.github.io/2016/04/27/goquery-number-of-children-nodes/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
goquery Get Number of Children Nodes
April 27, 2016
Get the number of children nodes via goquery in Golang (Go programming language).
- Get the number of all children nodes - Use Contents() method
- Get the number of element children nodes - Use Children() method
Install goquery:
$ go get -u github.com/PuerkitoBio/goquery
Source code:
node.go | repository | view raw
package main import ( "github.com/PuerkitoBio/goquery" "strings" ) const html = `<html> <head> <title>traverse</title> </head> <body> <div> Hello <span>World</span> <!-- Goquery --> </div> </body> </html>` func NumberOfChild(s *goquery.Selection) int { return s.Contents().Length() //return s.Contents().Size() } func NumberOfElementChild(s *goquery.Selection) int { return s.Children().Length() //return s.Children().Size() } func main() { doc, err := goquery.NewDocumentFromReader(strings.NewReader(html)) if err != nil { panic(err) } println(NumberOfChild(doc.Find("div"))) println(NumberOfElementChild(doc.Find("div"))) }
Output:
5 1
Tested on: Ubuntu Linux 16.04, Go 1.6.2.
References:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK