Created
November 12, 2016 10:25
-
-
Save ahmdrz/2f83c248377082fbd01ed4fdcd1de96a to your computer and use it in GitHub Desktop.
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 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