Parsing HTML in Go (Golang): Libraries and Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Parsing HTML in Go (Golang): Tools and Practical Examples



Go (Golang) is a compiled, statically typed language created at Google. Thanks to its high performance, built-in concurrency support, and rich standard library, Go has become one of the best languages for web scraping and HTML parsing. In this article, we will cover the main libraries for working with HTML documents in Go, provide code examples, and compare their capabilities.



Why Parse HTML in Go?

Parsing HTML is necessary for extracting data from web pages: news, prices, contact information, search results. Go is well-suited for this task thanks to:

  • High execution speed (compiles to machine code)
  • Easy network handling (built-in net/http package)
  • Simple DOM tree parsing via specialized libraries
  • Excellent multithreading support (goroutines and channels)


Main Libraries for Parsing HTML in Go



1. net/html — Standard Library

golang.org/x/net/html is a low-level tokenizer and HTML parser from the Go standard library. It provides full control over the parsing process but requires writing more code.

Example: extracting all links (<a href>) from an HTML document.

package main

import ( "fmt" "golang.org/x/net/html" "net/http" "log")

func main() { resp, err := http.Get("https://example.com") if err != nil { log.Fatal(err) } defer resp.Body.Close()

doc, err := html.Parse(resp.Body) 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 _, attr := range n.Attr { if attr.Key == "href" { fmt.Println(attr.Val) } } } for c := n.FirstChild; c != nil; c = c.NextSibling { f(c) } } f(doc)}

Pros: no need to install third-party packages, full control. Cons: lots of boilerplate code, inconvenient for complex selectors.



2. goquery — jQuery-like Syntax

github.com/PuerkitoBio/goquery is the most popular library for parsing HTML in Go. It provides an API similar to jQuery: Find(), Each(), Attr(), Text().

Example: parsing news headlines from a website.

package main

import ( "fmt" "github.com/PuerkitoBio/goquery" "log" "net/http")

func main() { resp, err := http.Get("https://habr.com/ru/news/") if err != nil { log.Fatal(err) } defer resp.Body.Close()

doc, err := goquery.NewDocumentFromReader(resp.Body) if err != nil { log.Fatal(err) }

doc.Find("h2.tm-article-snippet__title").Each(func(i int, s *goquery.Selection) { title := s.Find("a").Text() link, _ := s.Find("a").Attr("href") fmt.Printf("%d. %s Link: %s", i+1, title, link) })}

Pros: simple syntax, CSS selector support, rich documentation. Cons: not suitable for very large documents (loads everything into memory).



3. colly — Web Scraping Framework

github.com/gocolly/colly is a powerful data collection framework. It includes HTML parsing (based on goquery), request queue management, cookie handling, rate limiting, and much more.

Example: collecting all links from the first page of a website with a delay between requests.

package main

import ( "fmt" "github.com/gocolly/colly/v2")

func main() { c := colly.NewC

Recommendations