在goroutine執(zhí)行的過程中有额,需要進(jìn)行g(shù)oroutine的同步涛漂。Go語言提供了sync包和channel機(jī)制來解決goroutine的同步問題
1. sync.WaitGroup
// 代碼來自官方文檔
package main
import (
"fmt"
"io/ioutil"
"sync"
"net/http"
)
var wg sync.WaitGroup
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
func main() {
for _, url := range urls {
// Increment the WaitGroup counter.
wg.Add(1)
// Launch a goroutine to fetch the URL.
go func(url string) {
// Decrement the counter when the goroutine completes.
defer wg.Done()
// Fetch the URL.
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error is: ", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}(url)
}
// Wait for all HTTP fetches to complete.
wg.Wait()
}
<html>
<body>
Some stupid name....
Error is: Get http://www.google.com/: dial tcp 75.126.2.43:80: getsockopt: connection refused
Error is: Get http://www.golang.org/: dial tcp 216.239.37.1:80: getsockopt: connection refused
// 因?yàn)闆]有翻墻瑟俭。咬像。。
2. chan
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
var ch = make(chan string, len(urls))
func main() {
for _, url := range urls {
go func(url string) {
resp, err := http.Get(url)
if err != nil {
str := "Error is: " + err.Error()
ch <- str
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
ch <- string(body)
}(url)
}
for i := 0; i < len(urls); i++ {
str := <-ch
fmt.Println(str)
}
}
<html>
<body>
Some stupid name....
Error is: Get http://www.google.com/: dial tcp 173.252.102.16:80: getsockopt: connection refused
Error is: Get http://www.golang.org/: dial tcp 216.239.37.1:80: getsockopt: connection refused
channel是一種 阻塞管道 , 是自動阻塞 的. 如果 channel 滿了, 對channel放入數(shù)據(jù)的操作就會阻塞, 直到有某個(gè)routine從channel中取出數(shù)據(jù), 這個(gè)放入數(shù)據(jù)的操作才會執(zhí)行. 相反同理, 如果管道是空的, 一個(gè)從channel取出數(shù)據(jù)的操作就會阻塞,直到某個(gè)routine向這個(gè)channel中放入數(shù)據(jù), 這個(gè)取出數(shù)據(jù)的操作才會執(zhí)行(原理非常類似阻塞型socket的讀寫緩沖區(qū))