Skip to content
Shawn Wang edited this page Dec 3, 2019 · 1 revision

title: "" date: 2019-11-23 type: blog author: AppleBoy link: https://blog.wu-boy.com/2019/11/7557/ layout: post comments: true

Select in Golang 四大用法

本篇教學要帶大家認識 Go 語言Select 用法,相信大家對於 switch 並不陌生,但是 selectswitch 有個共同特性就是都過 case 的方式來處理,但是 select 跟 switch 處理的事情完全不同,也完全不相容。來看看 switch 有什麼特性: 各種類型及型別操作,接口 interface{} 型別判斷 variable.(type),重點是會依照 case 順序依序執行。底下看個例子:

package main

var ( i interface{} )

func convert(i interface{}) { switch t := i.(type) { case int: println("i is interger", t) case string: println("i is string", t) case float64: println("i is float64", t) default: println("type not found") } }

func main() { i = 100 convert(i) i = float64(45.55) convert(i) i = "foo" convert(i) convert(float32(10.0)) }

Clone this wiki locally