[Go] 解決空接口 interface{} cannot use (type []string) as type []interface {}


空接口 interface{}

可以存儲任何類型的數據

但是在和slice以及map配合時 ,要注意

[]interface{}  或者 map[string]interface{}

 

可能會犯這樣的錯誤 cannot use (type []string) as type []interface {}

不能將[]T 轉成 []interface  , 也不能將 map[string]T 轉成 map[string]interface{}

 

Go語言規范不允許這樣做,因為兩種類型在內存中沒有相同的表現形式。

需要單獨定義[]interface{}  map[string]interface{}

把想轉換的元素復制一遍到上面的類型里

 例如下面這樣的錯誤:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM