Advertisement

Go协程与协程池

阅读量:

1. Golang协程

  • golang与其他编程语言之间最显著的差异在于其特有的goroutine,即go语言中的协程,具体示例如下:
复制代码
 package main

    
  
    
 import "fmt"
    
 import "time"
    
  
    
 func go_worker(name string) {
    
 	for i:=0; i<10; i++ {
    
 		fmt.Println("this is go worker :" , name)
    
 	}
    
 }
    
  
    
 func main(){
    
 	go go_worker("lineshen")
    
 	go go_worker("glorialu")
    
  
    
 	time.Sleep(time.Second*5) // print effective
    
 	fmt.Println("main finished")
    
 }
    
    
    
    

输出:

复制代码
 this is go worker : lineshen

    
 this is go worker : lineshen
    
 this is go work

全部评论 (0)

还没有任何评论哟~