Go語言Map的使用

來源:互聯網
上載者:User

標籤:擷取   japan   代碼執行   hash   代碼   let   數組   函數   state   

Go 語言Map(集合)

Map 是一種無序的索引值對的集合。Map 最重要的一點是通過 key 來快速檢索資料,key 類似於索引,指向資料的值。

Map 是一種集合,所以我們可以像迭代數組和切片那樣迭代它。不過,Map 是無序的,我們無法決定它的返回順序,這是因為 Map 是使用 hash 表來實現的。

定義 Map

可以使用內建函數 make 也可以使用 map 關鍵字來定義 Map:


聲明變數,預設map是nil
var map_name = map[type]type
另外一種使用make建立
map_name := make(map[type]type)

如果不初始化 map,那麼就會建立一個 nil map。nil map 不能用來存放索引值對

下面執行個體示範了建立和使用map:

 1 package main 2  3 import ( 4     "fmt" 5 ) 6  7 func main(){ 8     //建立map 9     countryCapitalMap := make(map[string]string)10 11     //插入每個國家對應的首都12     countryCapitalMap["France"] = "Paris"13     countryCapitalMap["Italy"] = "Rome"14     countryCapitalMap["Japan"] = "Tokyo"15     countryCapitalMap["India"] = "New Delhi"16 17     //使用key輸出map的值18     fmt.Println("第一種輸出的方式")19     for country := range countryCapitalMap{20         fmt.Println("Capital of",country,"is",countryCapitalMap[country])21     }22 23     //直接輸出map的key和值24     fmt.Println("第二種輸出的方式")25     for k,v := range countryCapitalMap{26         fmt.Println("Capital of",k,"is",v)27     }28 29     //查看元素是否在map中,變數ok會返回true或者false,當查詢的key在map中則返回true並且captial會擷取到map中的值30     fmt.Println("***************************************")31     captial, ok := countryCapitalMap["United States"]32     if ok{33         fmt.Println("Capital of",captial,"is",countryCapitalMap[captial])34     }else{35         fmt.Println("Capital of United States is not present")36     }37 }

以上代碼執行返回的結果:

第一種輸出的方式Capital of France is ParisCapital of Italy is RomeCapital of Japan is TokyoCapital of India is New Delhi第二種輸出的方式Capital of France is ParisCapital of Italy is RomeCapital of Japan is TokyoCapital of India is New Delhi***************************************Capital of United States is not present
delete() 函數

delete() 函數用於刪除集合的元素, 參數為 map 和其對應的 key。執行個體如下:

package mainimport (    "fmt")func main(){    //建立map    countryCapitalMap := map[string] string {"France":"Paris","Italy":"Rome","Japan":"Tokyo","India":"New Delhi"}    fmt.Println("原始的map")    for k,v := range countryCapitalMap{        fmt.Println("Capital of",k,"is",v)    }    //刪除map中的France中的key和值    delete(countryCapitalMap,"France")    fmt.Println("刪除後的map")    for k,v := range countryCapitalMap{        fmt.Println("Capital of",k,"is",v)    }}

以上代碼執行返回的結果:

原始的mapCapital of Italy is RomeCapital of Japan is TokyoCapital of India is New DelhiCapital of France is Paris刪除後的mapCapital of Japan is TokyoCapital of India is New DelhiCapital of Italy is Rome

因為map的特性,排序是無序的,但是,我TMD就想按著map的key的順序列印怎麼搞,往下看!

 1 package main 2  3 import ( 4     "fmt" 5 ) 6  7 func main(){ 8     //建立map 9     countryCapitalMap := map[string] string {"France":"Paris","Italy":"Rome","Japan":"Tokyo","India":"New Delhi"}10     country_array := [] string {"France","Italy","Japan","India"}11 12     //根據自訂的數組的順序有序的列印map中的資訊13     for _,country := range country_array{14         fmt.Printf("Capital %v of  is %v \n",country,countryCapitalMap[country])15     }16 }

以上代碼執行返回的結果:

Capital France of  is Paris Capital Italy of  is Rome Capital Japan of  is Tokyo Capital India of  is New Delhi 

 

Go語言Map的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.