Skip to content

Instantly share code, notes, and snippets.

@haleyrc
Created December 26, 2019 17:43
Show Gist options
  • Save haleyrc/2d024a4a11b37d2f91799815759f71fa to your computer and use it in GitHub Desktop.
Save haleyrc/2d024a4a11b37d2f91799815759f71fa to your computer and use it in GitHub Desktop.
A quick and dirty way to get a webpage title in Go.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
)
func main() {
url := "https://www.google.com"
resp, err := http.Get(url)
if err != nil {
panic(err)
}
b, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
body := string(b)
// fmt.Println(body)
openTag := regexp.MustCompile(`<title[^>]*>`)
closeTag := regexp.MustCompile(`<\/title>`)
ot := openTag.FindStringIndex(body)
ct := closeTag.FindStringIndex(body)
left := ot[1]
right := ct[0]
fmt.Printf("%q", body[left:right])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment