Goroutine in go language learning

Time:2020-10-26

Coroutine

characteristic

  • Lightweight “thread”
  • Non preemptive multi task processing, in which coprocessor takes the initiative to hand over control
  • Compiler / interpreter / virtual machine level multitasking, non operating system
  • Multiple coroutines can be executed on one or more threads

Go keyword opens a coroutine

func main() {
	for i := 0; i < 10; i++ {
		go func(i int) {
			for {
				fmt.Println(i)
			}
		}(i)
	}
	time.Sleep(time.Millisecond)
}

Possible switching point of gorutine (transfer control)

  • I/O,select
  • channel
  • Waiting for lock
  • Function call (sometimes)
  • routime.Goshed()
  • It’s just a reference. It can’t guarantee the switch or not switch in other places

Recommended Today

Consul1.7 multi data center new hashicorp Learning Guide

More services, more problems With the popularity of Internet distributed system and microservice, how to improve the scalability and scalability between services? How to minimize the impact on dependent services when the server makes changes? How does the client develop the service without knowing? How to reduce configuration modification and overload nginx services behind the […]