[Golang] Number of Child Nodes via net/html Package


Introduction

Get the number of child nodes via Go net/html package. Equivalent to the following JavaScript code:

var c = document.getElementById("foo").childNodes.length;

Install net/html package

$ go get -u golang.org/x/net/html

Number of Child Nodes

package getelementbyid

import (
      "golang.org/x/net/html"
)

func numberOfChild(n *html.Node) int {
      if n == nil {
              return -1
      }

      count := 0
      for c := n.FirstChild; c != nil; c = c.NextSibling {
              count += 1
      }

      return count
}

Tested on: Ubuntu Linux 15.10, Go 1.6.1.


References:

[1]jquery iterate over elements - Google search
[2]
[3]github.com/PuerkitoBio/goquery - GoDoc
[4][Golang] Iterate over All DOM Elements in HTML
[5][Golang] getElementById via net/html Package