Skip to content

Instantly share code, notes, and snippets.

@ahmdrz
Created November 12, 2016 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmdrz/2f83c248377082fbd01ed4fdcd1de96a to your computer and use it in GitHub Desktop.
Save ahmdrz/2f83c248377082fbd01ed4fdcd1de96a to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
)
func getTitle(url string) (string, error) {
title := ""
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
regex := regexp.MustCompile(`<title>(.*)<\/title>`)
results := regex.FindStringSubmatch(string(body))
if len(results) > 0 {
title = results[len(results)-1]
}
return title, nil
}
func main() {
fmt.Println(getTitle("http://google.com"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment