GoQuery is a library for Go that brings a syntax and a set of features similar to jQuery to the Go language. It is used primarily for web scraping, as it allows developers to navigate and manipulate HTML documents in a convenient manner.
To install GoQuery in your Go project, you will need to have Go installed on your system. If you haven't already set up Go, you can download it from the official Go website (https://golang.org/dl/).
Once Go is installed, you can add GoQuery to your Go project by using the go get
command, which downloads and installs packages and dependencies into your Go workspace. Open your terminal or command prompt and run the following command:
go get github.com/PuerkitoBio/goquery
This command will download the GoQuery package along with its dependencies into your Go workspace.
To use GoQuery in your Go project, you'll need to import it in your Go files like this:
package main
import (
"fmt"
"log"
"net/http"
"github.com/PuerkitoBio/goquery"
)
func main() {
// Example: Scrape the title of a web page.
res, err := http.Get("http://example.com")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
// Find the title tag
title := doc.Find("title").Text()
fmt.Println("Page Title:", title)
}
The above example fetches the HTML content from http://example.com
, parses it using GoQuery, finds the <title>
tag, and prints out the title text.
Remember to manage your project's dependencies properly, especially if you're working with a team or aiming for reproducible builds. Since Go 1.11, the Go modules feature has been introduced, which is a dependency management system that makes it easier to manage dependencies.
If you're using Go modules, you should initialize a new module if you haven't already by running:
go mod init <module-name>
After you have run go get
to install GoQuery, you can find the dependency listed in your go.mod
file. This ensures that anyone else working with your project will get the same version of GoQuery when they build the project.
To upgrade GoQuery to a newer version later on, you can use:
go get -u github.com/PuerkitoBio/goquery
This will fetch the latest version of GoQuery and update your go.mod
file accordingly.
Remember to always check GoQuery's repository for the latest instructions and documentation: https://github.com/PuerkitoBio/goquery.