package main
import (
"fmt"
"regexp"
"net/http"
"io/ioutil"
)
func file_get_contents(url string) string{
r, e := http.Get(url)
if e != nil {
return ""
}
defer r.Body.Close()
c, e := ioutil.ReadAll(r.Body)
if e != nil {
return""
}
return string(c)
}
func get_title(url string,c chan string){
html:=file_get_contents(url)
r:= regexp.MustCompile(`<title>([^<].+?)<\/title>`)
rs:=r.FindStringSubmatch(html)
c<-rs[1]
}
func main() {
u:=[]string{}
u=append(u,"http://www.oschina.net/news/41110/mozilla-firefox-weibo")
u=append(u,"http://www.oschina.net/news/41080/windows-phone-porting-challenge")
u=append(u,"http://www.oschina.net/news/41093/music-copyright")
u=append(u,"http://www.oschina.net/news/41109/rainweather-updated")
u=append(u,"http://www.oschina.net/news/41091/visual-studio-2013")
u=append(u,"http://www.oschina.net/news/41090/mysql-5-6-12-ga")
u=append(u,"http://www.oschina.net/code/snippet_103482_15911")
u=append(u,"http://www.oschina.net/code/snippet_212240_21885")
u=append(u,"http://www.oschina.net/translate/php-best-practices")
u=append(u,"http://www.oschina.net/news/41053/whitehouse-programming-hackathon")
u=append(u,"http://www.oschina.net/news/41035/android-things-network")
u=append(u,"http://www.oschina.net/news/41037/github-announces-octokit")
u=append(u,"http://www.oschina.net/news/41032/linux-opensource-cpu-z")
u=append(u,"http://www.oschina.net/news/41051/writing-code-is-most-important-in-21-century")
u=append(u,"http://www.oschina.net/news/41077/15-page-transitions-effects-tutorials-in-css3-and-jquery")
l:=len(u)
c := make(chan string,2)
for i:=0;i<l;i++{
go get_title(u[i],c)
}
for i:=0;i<l;i++{
fmt.Println(<-c);
}
}
转载于:https://my.oschina.net/yecao/blog/152106