import (
"fmt"
"reflect"
)
func main() {
//定义一个接口类型的切片
in := make([]interface{},3)
m1 := make(map[string]string)
m2 := make(map[string][]byte)
//初始化键值对
m1 = map[string]string{
"teleplay1": "情定大饭店",
"teleplay2": "天国的阶梯",
"teleplay3": "爱在哈佛",
"teleplay4": "我的女孩",
"teleplay5": "寻子三万里",
"teleplay6": "仙女与骗子",
"teleplay7": "青青草",
"teleplay8": "综艺 - 情书",
"teleplay9": "豪杰春香",
"teleplay10": "蓝色生死恋",
"teleplay11": "浪漫满屋",
"teleplay12": "城市猎人",
"teleplay13": "顺风妇产科",
"teleplay14": "综合医院",
"teleplay15": "白色巨塔",//每一行必须要有逗号
}
m1["teleplay16"] = "外科医生奉达熙" //初始化键值对
m2["song1"] = []byte{'F','a','l','i','n','g',' ','I','L','o','v','e'}
m2["song2"] = []byte{68,114,101,97,109,115,32,67,111,84,117,101}
in[0] = m1
in[1] = m2
fmt.Println("in[0] =",in[0])
fmt.Println("==============================================================================\n")
fmt.Println("in[1] =",in[1])
fmt.Println("==============================================================================\n")
//初始化键值对
in[2] = map[string]string{
"song3": "Yesterday Once More",
}
fmt.Println(in)
// 如何用 in 直接输出m2["song2"]的值?
fmt.Println("\n",`如何用 in 直接输出m2["song2"]的值?`)
fmt.Println("==============================================================================")
fmt.Println("in[1] =",in[1])
fmt.Println("in[1] Type =",reflect.TypeOf(in[1]))
fmt.Println(`in[1].(map[string][]byte) =`,in[1].(map[string][]byte))
fmt.Println(`直接输出m2["song2"]的值 =`,(in[1].(map[string][]byte))["song2"])
fmt.Println(`直接输出m2["song2"]的值 =`,string((in[1].(map[string][]byte))["song2"])) // byte[]转字符串
fmt.Println(`直接输出m2["song2"]的第1个字符 =`,(in[1].(map[string][]byte))["song2"][1]) // 输出114表示字母D
fmt.Println("==============================================================================")
fmt.Println("in[2] =",in[2])
fmt.Println((in[2].(map[string]string))["song3"])
}
控制台打印输出以下信息:
2017/07/01 20:37:22 server.go:73: Using API v1
2017/07/01 20:37:22 debugger.go:97: launching process with args: [/root/code/go/src/contoso.org/book/debug]
API server listening at: 127.0.0.1:2345
2017/07/01 20:37:22 debugger.go:505: continuing
in[0] = map[teleplay2:天国的阶梯 teleplay6:仙女与骗子 teleplay11:浪漫满屋 teleplay12:城市猎人 teleplay15:白色巨塔 teleplay16:外科医生奉达熙 teleplay1:情定大饭店 teleplay4:我的女孩 teleplay7:青青草 teleplay10:蓝色生死恋 teleplay14:综合医院 teleplay3:爱在哈佛 teleplay5:寻子三万里 teleplay8:综艺 - 情书 teleplay9:豪杰春香 teleplay13:顺风妇产科]
==============================================================================
in[1] = map[song1:[70 97 108 108 105 110 103 32 73 110 32 76 111 118 101] song2:[68 114 101 97 109 115 32 67 111 109 101 32 84 114 117 101]]
==============================================================================
[map[teleplay2:天国的阶梯 teleplay6:仙女与骗子 teleplay11:浪漫满屋 teleplay12:城市猎人 teleplay15:白色巨塔 teleplay16:外科医生奉达熙 teleplay1:情定大饭店 teleplay4:我的女孩 teleplay7:青青草 teleplay10:蓝色生死恋 teleplay14:综合医院 teleplay3:爱在哈佛 teleplay5:寻子三万里 teleplay8:综艺 - 情书 teleplay9:豪杰春香 teleplay13:顺风妇产科] map[song2:[68 114 101 97 109 115 32 67 111 109 101 32 84 114 117 101] song1:[70 97 108 108 105 110 103 32 73 110 32 76 111 118 101]] map[song3:Yesterday Once More]]
如何用 in 直接输出m2["song2"]的值?
==============================================================================
in[1] = map[song1:[70 97 108 108 105 110 103 32 73 110 32 76 111 118 101] song2:[68 114 101 97 109 115 32 67 111 109 101 32 84 114 117 101]]
in[1] Type = map[string][]uint8
in[1].(map[string][]byte) = map[song1:[70 97 108 108 105 110 103 32 73 110 32 76 111 118 101] song2:[68 114 101 97 109 115 32 67 111 109 101 32 84 114 117 101]]
直接输出m2["song2"]的值 = [68 114 101 97 109 115 32 67 111 109 101 32 84 114 117 101]
直接输出m2["song2"]的值 = Dreams Come True
直接输出m2["song2"]的第1个字符 = 114
==============================================================================
in[2] = map[song3:Yesterday Once More]
Yesterday Once More
请看如下技巧描述: