Fanout 可以让编写平行代码更加容易。Fanout 的代码是可以重复使用的,所以你可以很容易地就写下平行代码,而不用担心 fatal error: all goroutines are asleep - deadlock!
示例代码:
<pre class="brush:cpp ;toolbar: true; auto-links: false;">for _, word := range domainWords {
if strings.TrimSpace(word) == "" {
continue
}
py := pinyin.Convert(word)
pydowncase := strings.ToLower(py)
domain := pydowncase + ".com"
outr, err := domainAvailable(word, domain)
if err != nil {
fmt.Println("Error: ", err)
continue
}
if outr.available {
fmt.Printf("[Ohh Yeah] %s %s\n", outr.word, outr.domain)
continue
}
fmt.Printf("\t\t\t %s %s %s\n", outr.word, outr.domain, outr.summary)
}
type checkResult struct {
word string
domain string
available bool
summary string
}
func domainAvailable(word string, domain string) (ch checkResult, err error) {
var summary string
var output []byte
ch.word = word
ch.domain = domain
cmd := exec.Command("whois", domain)
output, err = cmd.Output()
if err != nil {
fmt.Println(err)
return
}
outputstring := string(output)
if strings.Contains(outputstring, "No match for \"") {
ch.available = true
return
}
summary = firstLineOf(outputstring, "Registrant Name") + " => "
summary = summary + firstLineOf(outputstring, "Expiration Date")
ch.summary = summary
return
}</pre>