在日常編程中,除了使用內置的資料型別,還會使用一些復雜的自定義資料型別,比如map K為string,V為陣列,先了解一下go對map的基本設定:
map的key可以是任意內置的資料型別(如int),或者其它可以通過"=="進行等值比較的資料型別,如interface和指標可以,slice、陣列、map、struct型別都不能作為key,
map的value卻可以是任意型別,例如嵌套一個slice到map中:
func main() {
testmap := map[string][]string{}
testmap["1"] = []string{"001", "002"}
fmt.Println(testmap["1"])
}
run
? testgo go run main.go
[001 002]
遍歷:
func main(){
testmap := map[string][]string{}
testmap["1"] = []string{"001", "002"}
for order := range testmap{
fmt.Println(order, testmap[order])
fmt.Println(testmap[order][0])
fmt.Println(testmap[order][1])
}
}
run
? testgo go run main.go
1 [001 002]
001
002
參考
https://studygolang.com/articles/16426
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/1894.html
標籤:Go
上一篇:Go Protobuf 參考教程 - Grpc Go C++ 通信
下一篇:go 動態陣列 二維動態陣列
