1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package main
import "fmt"
func main() {
var country = map[string]string{ "China": "Beijing", "Japan": "Tokyo", "India": "New Delhi", "France": "Paris", "Italy": "Rome", } fmt.Println(country)
rating := map[string]float64{"c": 5, "Go": 4.5, "Python": 4.5, "C++": 3} fmt.Println(rating)
countryMap := make(map[string]string) countryMap["China"] = "Beijing" countryMap["Japan"] = "Tokyo" countryMap["India"] = "New Delhi" countryMap["France"] = "Paris" countryMap["Italy"] = "Rome"
for k, v := range countryMap { fmt.Println("国家", k, "首都", v) } fmt.Println("-----------")
for _, v := range countryMap { fmt.Println("国家", "首都", v) } fmt.Println("-----------")
for k := range countryMap { fmt.Println("国家", k , "首都", countryMap[k]) } fmt.Println("-----------")
value , ok := countryMap["England"] fmt.Printf("%q \n" , value) fmt.Printf("%T , %v \n" , ok , ok) if ok { fmt.Println("首都:" , value) } else { fmt.Println("首都信息未检索到!") }
if value ,ok :=countryMap["USA"];ok { fmt.Println("首都:" , value) } else { fmt.Println("首都信息未检索到!") } }
|