当前位置:网站首页>go_ keyword

go_ keyword

2022-06-24 21:06:00 AcTarjan

switch

  • Look for a match case, Exit after execution switch
func judgeType(i interface{
    }) {
    
	switch i.(type) {
    
	case bool:
		fmt.Println("bool")
	case int:
		fmt.Println("int")
	case string:
		fmt.Println("string")
	default:
		fmt.Println("unknown")
	}
}

select

  • select It can only be used for channel The operation of ,channel Don't block , Then case It can be executed
  • If there are more than one case Can be executed ,select One of them will be selected randomly and fairly , Others will not be executed
  • If there is no executable case sentence , But there are default sentence , Then it will carry out default The action of
  • If there is no executable case sentence , And there's no default sentence ,select Will block , Until some case It can be executed
ch := make(chan int,1)
select {
    
case <-ch:
	fmt.Println(1)
case ch<-2:
	fmt.Println(2)
default:
	fmt.Println(3)
}
/******************* 2 *******************/
原网站

版权声明
本文为[AcTarjan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211319438329.html