channel可以是各種數(shù)據(jù)類型:
(map類型)
(struct類型)
(接口類型)
package main
import "fmt"
type student struct {
name string
}
func main() {
var stuChan chan interface{}
//空interface類型可以是任意類型
stuChan = make(chan interface{}, 10)
stu := student{name:"stu01"}
stuChan <- &stu
var stu01 interface{}
stu01 = <- stuChan
fmt.Println(stu01)
var stu02 *student
//將stu01由interface轉(zhuǎn)換為結(jié)構(gòu)體類型指針
stu02, ok := stu01.(*student)
if !ok {
fmt.Println("不可以轉(zhuǎn)換")
return
}
fmt.Println(stu02)
}
輸出:
&{stu01}
&{stu01}
Process finished with exit code 0
channel關(guān)閉,判斷:
package main
import "fmt"
func main() {
var ch chan int
ch = make(chan int, 10)
for i := 0; i < 10; i++ {
ch <- i
}
close(ch)
for {
var b int
//從chan取數(shù)據(jù)的時(shí)候第二個(gè)參數(shù)用來判斷是否成功
b, ok := <- ch
if ok == false {
fmt.Println("chan is closed")
return
}
fmt.Println(b)
}
}
輸出:
0
1
2
3
4
5
6
7
8
9
chan is closed
range遍歷chan:
package main
import "fmt"
func main() {
var ch chan int
ch = make(chan int, 1000)
for i := 0; i < 1000; i++ {
ch <- i
}
close(ch)
for v := range ch {
fmt.Println(v)
}
}
關(guān)閉chan,goroutine同步案例:
package main
import (
"fmt"
)
func calc(taskChan chan int, resChan chan int, exitChan chan bool) {
for v := range taskChan {
flag := true
for i := 2; i < v; i++ {
if v%i == 0 {
flag = false
break
}
}
if flag {
resChan <- v
}
}
fmt.Println("exit")
//每個(gè)calc的協(xié)程(goroutine)結(jié)束后寫入true到名為exitChan的chan里。代表該goroutine結(jié)束了
exitChan <- true
}
func main() {
intChan := make(chan int, 1000)
resultChan := make(chan int, 1000)
exitChan := make(chan bool, 8)
go func() {
for i := 0; i < 10000; i++ {
intChan <- i
}
close(intChan)
}()
for i := 0; i < 8; i++ {
go calc(intChan, resultChan, exitChan)
}
//等待所有計(jì)算的goroutine全部退出
go func() {
for i := 0; i < 8; i++ {
<-exitChan
fmt.Println("wait goroute ", i, " exited")
}
//8個(gè)goroutine結(jié)束后關(guān)閉resultChan管道
close(resultChan)
}()
//resultChan管道關(guān)閉后,range才能在遍歷完后結(jié)束
for v := range resultChan {
fmt.Println(v)
}
}
再來個(gè)同步channel的例子:
package main
import (
"fmt"
)
func send(ch chan int, exitChan chan bool) {
for i := 0; i < 10; i++ {
ch <- i
}
close(ch)
exitChan <- true
}
func recv(ch chan int, exitChan chan bool) {
for {
v, ok := <- ch
if !ok {
break
}
fmt.Println(v)
}
exitChan <- true
}
func main() {
ch := make(chan int, 10)
exitChan := make(chan bool, 2)
go send(ch, exitChan)
go recv(ch, exitChan)
var total = 0
for _ = range exitChan {
total++
if total == 2 {
break
}
}
}
輸出:
0
1
2
3
4
5
6
7
8
9
Process finished with exit code 0
channel的只讀,只寫(讀函數(shù)里只準(zhǔn)讀,寫函數(shù)里只準(zhǔn)寫。來進(jìn)行規(guī)范,防止出錯(cuò)):
package main
import "fmt"
func send(ch chan<- int, exitChan chan struct{}) {
for i := 0; i < 10; i++ {
ch <- i
}
close(ch)
var a struct{}
exitChan <- a
}
func recv(ch <-chan int, exitChan chan struct{}) {
for {
v, ok := <-ch
if !ok {
break
}
fmt.Println(v)
}
var a struct{}
exitChan <- a
}
func main() {
var ch chan int
ch = make(chan int, 10)
exitChan := make(chan struct{}, 2)
go send(ch, exitChan)
go recv(ch, exitChan)
var total = 0
for _ = range exitChan {
total++
if total == 2 {
break
}
}
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前名稱:goroutine/channel(1)-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://aaarwkj.com/article8/cchsop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、手機(jī)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、外貿(mào)建站、電子商務(wù)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容