1. 程式人生 > 其它 >GoLang interface{} 轉他對應的型別

GoLang interface{} 轉他對應的型別

技術標籤:golanginterface

GoLang中前端提交的資料 map[string]interface{} 轉對應的型別方法。
比如前端提交的字串要轉成數字,但可能是字串提交也可能是int提交。


var value interface{}
var id int 
switch vt := value.(type) {
	case string:
		id , _ = strconv.Atoi(vt)
	case float64:
		id = int(vt)
	case float33:
		id = int(vt)