I am using the built-in html library in Golang.
Here's the code to reproduce the issue:
package main
import (
"fmt"
"log"
"net/http"
"golang.org/x/net/html"
)
const url = "https://google.com"
func main() {
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalf("Status code error: %d %s", resp.StatusCode, resp.Status)
}
h := html.NewTokenizer(resp.Body)
for {
if h.Next() == html.ErrorToken {
break
}
l := len(h.Token().Attr)
if l != 0 {
fmt.Println("=======")
fmt.Println("Length", l) // greater than 0
fmt.Println("Attr", h.Token().Attr) // empty all the times
}
}
}
Here's what the output looks like
=======
Length 2
Attr []
typeof Attr []html.Attribute
=======
Length 8
Attr []
typeof Attr []html.Attribute
=======
Length 1
Attr []
typeof Attr []html.Attribute
=======
Length 1
Attr []
typeof Attr []html.Attribute
go version
go version go1.17.7 linux/amd64
Why does Go think the length of h.Token().Attr is non-zero here when the h.Token().Attr is empty?
P.S.: saving the output of h.Token().Attr and using it for len and printing the contents makes everything work fine
Code:
package main
import (
"fmt"
"log"
"net/http"
"golang.org/x/net/html"
)
const url = "https://google.com"
func main() {
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalf("Status code error: %d %s", resp.StatusCode, resp.Status)
}
h := html.NewTokenizer(resp.Body)
for {
if h.Next() == html.ErrorToken {
break
}
attrs := h.Token().Attr // save the output here and use it everywhere else
l := len(attrs)
if l != 0 {
fmt.Println("=======")
fmt.Println("Length", l)
fmt.Println("Attr", attrs)
}
}
}
Output
Length 3
Attr [{ value AJiK0e8AAAAAYtZT7PXDBRBC2BJawIxezEfmIL6Aw5Uy} { name iflsig} { type hidden}]
=======
Length 4
Attr [{ class fl sblc} { align left} { nowrap } { width 25%}]
=======
Length 1
Attr [{ href /advanced_search?hl=en-IN&authuser=0}]
=======
Length 4
Attr [{ id gbv} { name gbv} { type hidden} { value 1}]