Commit 63802148 by huangchengyi

1.0

parent 1320a5e2
...@@ -12,7 +12,7 @@ const goods_slice_count = 10 //每多少个型号id开启一个协程 ...@@ -12,7 +12,7 @@ const goods_slice_count = 10 //每多少个型号id开启一个协程
/* /*
查询商品详情(自营或者联营) 查询商品详情(自营或者联营)
@doc http://192.168.2.232:3000/project/128/interface/api/649 @doc http://192.168.1.237:3000/project/128/interface/api/649
@param goods_id 支持批量,不建议超过40个,超过会导致处理时间超过2秒,进程异常 : 74564,65897,456464131 @param goods_id 支持批量,不建议超过40个,超过会导致处理时间超过2秒,进程异常 : 74564,65897,456464131
@param power[newCustomer] 是否获取新客价 :true @param power[newCustomer] 是否获取新客价 :true
......
/*
go通道选择 Select
go 的select关键字可以让你同时等待多个通道操作
将协程
通道和select结合起来构成了go的一个强大特性
*/
package main
import (
"fmt"
"github.com/syyongx/php2go"
"time"
)
func main() {
c1 := make(chan string)
go func() {
fmt.Println(php2go.Time())
time.Sleep(time.Second * 1)
c1 <- "one"
}()
go func() {
fmt.Println(php2go.Time())
time.Sleep(time.Second * 2)
c1 <- "two"
}()
go func() {
fmt.Println(php2go.Time())
time.Sleep(time.Second * 10)
c1 <- "10"
}()
/*
如我们所期望的 程序输出了正确的值 对于select语句而言
它不断地检测通道是否有值过来 一旦有值过来立刻获取输出
*/
//我们使用select来等待通道的值 然后输出
for i := 0; i < 3; i++ {
select {
case x := <-c1:
fmt.Println(x)
case <- time.After(time.Second *2):
fmt.Println("read time out")
}
}
}
\ No newline at end of file
package main
import (
"errors"
"fmt"
"time"
)
func main() {
// 构建一个通道
ch := make(chan string)
// 开启一个并发匿名函数
go func() {
ch <- "gagagagagga111"
}()
go func() {
ch <- "gagagagagga223"
}()
go func() {
time.Sleep(1*time.Second)
ch <- "gagagagagga44444"
}()
//无限循环从通道中读取数据
for {
i,err := ReadWithSelect(ch)
//i, ok := <-ch
if err != nil {
fmt.Println(err)
break
}
fmt.Println(i)
}
}
//使用Select+超时改善无阻塞读写
func ReadWithSelect(ch chan string) (x string, err error) {
timeout := time.NewTimer(time.Microsecond * 50000).C //等待5s
select {
case x = <-ch:
return x, nil
case <-timeout.C:
return "", errors.New("read time out")
}
}
func WriteChWithSelect(ch chan int) error {
timeout := time.NewTimer(time.Microsecond * 500)
select {
case ch <- 1:
return nil
case <-timeout.C:
return errors.New("write time out")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment