商君

导航

2018年10月22日 #

Go Example--锁

摘要: ```go package main import ( "fmt" "math/rand" "runtime" "sync" "sync/atomic" "time" ) func main() { var state = make(map[int]int) var mutex = &sync.Mu 阅读全文

posted @ 2018-10-22 19:57 漫步者01 阅读(103) 评论(0) 推荐(0) 编辑

Go Example--原子计数器

摘要: ```go package main import ( "fmt" "runtime" "sync/atomic" "time" ) func main() { var ops uint64 = 0 for i := 0; i 阅读全文

posted @ 2018-10-22 19:44 漫步者01 阅读(349) 评论(0) 推荐(0) 编辑

Go Example--限速

摘要: ```go package main import ( "fmt" "time" ) func main() { requests := make(chan int, 5) for i := 1; i 阅读全文

posted @ 2018-10-22 19:35 漫步者01 阅读(115) 评论(0) 推荐(0) 编辑

2018年10月19日 #

Go Example--工作池

摘要: ```go package main import ( "fmt" "time" ) func main() { jobs :=make(chan int,100) results := make(chan int,100) //启动3个协程 for w:=1;w 阅读全文

posted @ 2018-10-19 17:15 漫步者01 阅读(85) 评论(0) 推荐(0) 编辑

Go Example--打点器

摘要: ```go package main import ( "time" "fmt" ) func main() { // 定时器 是当你想要在未来某一刻执行一次时使用的 - 打点器 // 则是当你想要在固定的时间间隔重复执行准备的。这里是一个打点器的例子, // 它将定时的执行,直到我们将它停止。 ticker := time.NewTicker(time.Millisecond*5... 阅读全文

posted @ 2018-10-19 16:52 漫步者01 阅读(150) 评论(0) 推荐(0) 编辑

Go Example--定时器

摘要: ```go package main import ( "fmt" "time" ) func main() { //定时器2s timer1 := time.NewTimer(time.Second 2) //读取通道,阻塞2s 阅读全文

posted @ 2018-10-19 16:40 漫步者01 阅读(105) 评论(0) 推荐(0) 编辑

Go Example--通道遍历

摘要: ```go package main import ( "fmt" ) func main() { queue := make(chan string, 2) queue 阅读全文

posted @ 2018-10-19 16:27 漫步者01 阅读(125) 评论(0) 推荐(0) 编辑

Go Example--关闭通道

摘要: ```go package main import ( "fmt" ) func main() { jobs := make(chan int, 5) done := make(chan bool) go func() { for { //读取通道方式, val,ok := 阅读全文

posted @ 2018-10-19 16:19 漫步者01 阅读(169) 评论(0) 推荐(0) 编辑

Go Example--通道非阻塞

摘要: ```go package main import ( "fmt" ) func main() { messages := make(chan string) signals := make(chan bool) //常规的通过通道发送和接收数据是阻塞的。然而,我们可以使用带一个 default子句 阅读全文

posted @ 2018-10-19 15:45 漫步者01 阅读(121) 评论(0) 推荐(0) 编辑

Go Example--超时处理

摘要: ```go package main import ( "fmt" "time" ) func main() { c1 := make(chan string, 1) go func() { time.Sleep(time.Second 2) c1 阅读全文

posted @ 2018-10-19 15:22 漫步者01 阅读(106) 评论(0) 推荐(0) 编辑