在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ GO/ Go語言映射
Go panic錯誤處理實例
Go命令行參數(shù)實例
Go可變參數(shù)的函數(shù)實例
Go通道同步實例
Go非阻塞通道操作實例
Go指針實例
Go數(shù)字解析實例
Go語言指針
Go超時(timeouts)實例
Go速率限制實例
Go信號實例
Go Base64編碼實例
Go計時器實例
Go命令行標志實例
Go原子計數(shù)器實例
Go語言切片
Go隨機數(shù)實例
Go語言類型轉(zhuǎn)換
Go排序?qū)嵗?/span>
Go時間格式化/解析實例
Go URL解析實例
Go字符串函數(shù)實例
Go語言常量
Go for循環(huán)語句實例
Go函數(shù)多個返回值實例
Go切片實例
Go行過濾器實例
Go語言接口
Go語言數(shù)組
Go語言變量
Go字符串格式化實例
Go斷續(xù)器實例
Go if/else語句實例
Go通道緩沖實例
Go錯誤實例
Go語言映射
Go執(zhí)行過程實例
Go函數(shù)實例
Go有狀態(tài)的goroutines實例
Go按自定義函數(shù)排序?qū)嵗?/span>
Go語言作用域規(guī)則
Go時代(Epoch)實例
Go變量實例
Go互斥體實例
Go語言范圍(range)
Go程序?qū)嵗?/span>
Go語言入門
Go通道路線實例
Go閉包(匿名函數(shù))實例
Go Select實例
Go通道范圍實例
Go集合函數(shù)實例
Hello World程序?qū)嵗?/span>
Go環(huán)境變量實例
Go語言運算符
Go讀取文件實例
Go延遲(defer)實例
Go SHA1哈希實例
Go語言條件和決策
Go語言錯誤處理
Go通道實例
Go指針實例
Go時間日期實例
Go語言字符串
Go語言循環(huán)
Go語言基礎(chǔ)語法
Go語言開發(fā)環(huán)境安裝配置
Go常量實例
Go語言結(jié)構(gòu)體
Go寫文件實例
Go正則表達式實例
Go JSON實例
Go語言教程
Go關(guān)閉通道實例
Go接口實例
Go語言遞歸
Go switch語句實例
Go函數(shù)遞歸實例
Go退出程序?qū)嵗?/span>
Go語言程序結(jié)構(gòu)
Go范圍實例
Go語言函數(shù)
Go工作池實例
Go語言數(shù)據(jù)類型

Go語言映射

Go提供了另一個重要的數(shù)據(jù)類型:映射(Map),它將唯一鍵映射到值。 鍵是用于在檢索值的對象。 給定一個鍵和一個值就可以在Map對象中設(shè)置值。設(shè)置存儲值后,就可以使用其鍵檢索它對應(yīng)的值了。

定義映射

必須要使用make函數(shù)來創(chuàng)建映射。

/* declare a variable, by default map will be nil*/
var map_variable map[key_data_type]value_data_type

/* define the map as nil map can not be assigned any value*/
map_variable = make(map[key_data_type]value_data_type)

示例

以下示例說明了映射的創(chuàng)建和使用。

package main

import "fmt"

func main() {
   var countryCapitalMap map[string]string
   /* create a map*/
   countryCapitalMap = make(map[string]string)

   /* insert key-value pairs in the map*/
   countryCapitalMap["France"] = "Paris"
   countryCapitalMap["Italy"] = "Rome"
   countryCapitalMap["Japan"] = "Tokyo"
   countryCapitalMap["India"] = "New Delhi"

   /* print map using keys*/
   for country := range countryCapitalMap {
      fmt.Println("Capital of",country,"is",countryCapitalMap[country])
   }

   /* test if entry is present in the map or not*/
   capital, ok := countryCapitalMap["United States"]
   /* if ok is true, entry is present otherwise entry is absent*/
   if(ok){
      fmt.Println("Capital of United States is", capital)  
   }else {
      fmt.Println("Capital of United States is not present") 
   }
}

當上述代碼編譯和執(zhí)行時,它產(chǎn)生以下結(jié)果:

Capital of India is New Delhi
Capital of France is Paris
Capital of Italy is Rome
Capital of Japan is Tokyo
Capital of United States is not present

delete()函數(shù)

delete()函數(shù)用于從映射中刪除項目。它需要映射以及指定要刪除的相應(yīng)鍵。 以下是示例:

package main

import "fmt"

func main() {   
   /* create a map*/
   countryCapitalMap := map[string] string {"France":"Paris","Italy":"Rome","Japan":"Tokyo","India":"New Delhi"}

   fmt.Println("Original map")   

   /* print map */
   for country := range countryCapitalMap {
      fmt.Println("Capital of",country,"is",countryCapitalMap[country])
   }

   /* delete an entry */
   delete(countryCapitalMap,"France");
   fmt.Println("Entry for France is deleted")  

   fmt.Println("Updated map")   

   /* print map */
   for country := range countryCapitalMap {
      fmt.Println("Capital of",country,"is",countryCapitalMap[country])
   }
}

當上述代碼編譯和執(zhí)行時,它產(chǎn)生以下結(jié)果:

Original Map
Capital of France is Paris
Capital of Italy is Rome
Capital of Japan is Tokyo
Capital of India is New Delhi
Entry for France is deleted
Updated Map
Capital of India is New Delhi
Capital of Italy is Rome
Capital of Japan is Tokyo

上一篇:Go接口實例下一篇:Go指針實例