```go
package main
import (
"fmt"
_ "reflect"
"sync"
"time"
)
func writeData(intChan chan int) {
for i := 1; i <= 8000; i++ {
intChan <- i
//fmt.Printf("writeData()写入数据%v", i)
}
close(intChan)
//fmt.Println(intChan)
}
func judgData(intChan chan int, resultChan chan int, exitChan chan bool, done func()) {
defer done()
time.Sleep(time.Millisecond * 1000)
flag := true
//time.Sleep(time.Second *1)
for {
num, ok := <-intChan
if !ok {
fmt.Println("已从intChan读取所有数据")
break
}
for i := 2; i < num; i++ {
if num % i == 0 {
flag = false
break
}
if flag {
resultChan <- num
fmt.Printf("添加%v到resultChan\n", num)
}
}
}
fmt.Println("协程执行完毕")
exitChan <- true
}
func main() {
var wg sync.WaitGroup
intChan := make(chan int, 10000)
resultChan := make(chan int, 20000)
exitChan := make(chan bool, 8)
go writeData(intChan)
for i := 0; i < 8; i++ {
go judgData(intChan, resultChan, exitChan, wg.Done)
}
//go func() {
// for i := 0 ; i < 8 ; i ++ {
// key := <-exitChan
// fmt.Print(key)
// }
// close(resultChan)
//}()
wg.Add(8)
wg.Wait()
close(resultChan)
func() {
for i := 0; i < 8; i++ {
key := <-exitChan
fmt.Printf("key=%v\n", key)
}
}()
for {
res, ok := <-resultChan
if !ok {
break
}
fmt.Printf("素数=%d\n", res)
}
}
```
学到了,谢谢您,还能请教您一个小问题么,我的一段代码
````go
func (this *ExitMes) ExitMesProcess(mes *message.Message) (err error) {
fmt.Println(mes)
var exitMes message.ExitMes
err = json.Unmarshal([]byte(mes.Data), &exitMes)
fmt.Println(err)
data, err := json.Marshal(exitMes)
fmt.Println(string(data))
//println(*exitMes)
if err != nil {
err = model.ERROR_JSON_UNMARSHAL
return
}
//根据userId将用户从在线列表中剔除
userMgr.DelOnlineUser(exitMes.User.UserId)
//将这条用户退出消息广播给其他在线用户
sp := SmsProcess{}
//println(mes)
sp.SendGroupSms(mes)
return
}
````
然而反序列的值无法写入到exitMes这个变量中
#9
更多评论