Created
December 26, 2019 17:43
-
-
Save haleyrc/2d024a4a11b37d2f91799815759f71fa to your computer and use it in GitHub Desktop.
A quick and dirty way to get a webpage title in Go.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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