bin/go/fetchtitle.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package main import ( "bytes" "fmt" "io" "net/http" "os" "regexp" ) func main() { re := regexp.MustCompile(`<title>\n*(.*?)\n*</title>`) command := os.Args[0] urls := os.Args[1:] for _, url := range urls { resp, err := http.Get(url) if err != nil { fmt.Fprintf(os.Stderr, "%s: %v\n", command, err) os.Exit(1) } defer resp.Body.Close() b := bytes.NewBuffer([]byte{}) _, err = io.Copy(b, resp.Body) matches := re.FindSubmatch(b.Bytes()) fmt.Printf("%s", matches[1]) if err != nil { fmt.Fprintf(os.Stderr, "%s: reading %s: %v\n", command, url, err) os.Exit(1) } } } |