摘要:
package main import ( "fmt" "math/rand" ) type Job struct { Number int Id int } type Result struct { job *Job sum int } func calc(job *Job, result cha 阅读全文
摘要:
package main import ( "fmt" "time" ) func sendData(sendch chan<- int) { fmt.Println("go sendData", sendch) sendch <- 10 //<-sendch } func readData(sen 阅读全文
摘要:
package main import ( "fmt" "runtime" "time" ) var i int func calc(i1 int) { for ;i < 10; i++ { // 死循环,不会自己结束的 //i ++ fmt.Printf("全局i:%d >>> 局部i1:%d g 阅读全文
摘要:
package main import ( "fmt" "sync" "time" ) func process(i int, wg *sync.WaitGroup) { fmt.Println("started Goroutine ", i) time.Sleep(2 * time.Second) 阅读全文
摘要:
map和切片结合使用 package main import( "fmt" "math/rand" "time" ) func testSliceMap(){ rand.Seed(time.Now().UnixNano()) var s []map[string]int s = make([]map 阅读全文
摘要:
不带缓存区的, 必需要goroutine等待着从队列中取,才能放入无缓存的队列中 package main import ( "fmt" "time" ) func produce(c chan int) { fmt.Println("produce start") time.Sleep(time. 阅读全文
摘要:
用死循环的方式从ch中获取数据, 但是得自己判断ch是否已经取空了 点击查看代码 package main import ( "fmt" ) func producer(chnl chan int) { for i := 0; i < 10; i++ { chnl <- i } defer clos 阅读全文
摘要:
点击查看代码 package main import ( "fmt" "time" ) func producer(chnl chan int) { for i := 0; i < 10; i++ { chnl <- i time.Sleep(time.Second) } close(chnl) } 阅读全文
摘要:
点击查看代码 package main import ( "fmt" "time" ) func write(ch chan int) { for i := 0; i < 5; i++ { ch <- i fmt.Println("successfully wrote", i, "to ch") } 阅读全文
摘要:
利用goroutine等待的取出元素的时机,没有元素取出来,就一直阻塞,等待goroutine协程完成,取出队列中的元素,该进程才结束 点击查看代码 package main import ( "fmt" "time" ) func hello(c chan bool) { time.Sleep(5 阅读全文
摘要:
点击查看代码 package main import ( "fmt" "time" ) func numbers() { for i := 1; i <= 5; i++ { time.Sleep(250 * time.Millisecond) fmt.Printf("%d ", i) } } fun 阅读全文