Golang - concurrency 之 Goroutines

 

A goroutine is a function that is capable of running concurrently with other functions. To create a goroutine we use the keyword go followed by a function invocation:

 

 

package main

import "fmt"

func f(n int) {
  for i := 0; i < 10; i++ {
    fmt.Println(n, ":", i)
  }
}

func main() {
  go f(0)
  var input string
  fmt.Scanln(&input)
}

  结果:

 

  0 : 0
  0 : 1

 多次运行的结果

   0 : 0
  0 : 1
  0 : 2
  0 : 3
  0 : 4
  0 : 5
  0 : 6

  

posted @ 2017-11-12 17:09  烤酸奶  阅读(174)  评论(0编辑  收藏  举报