婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 淺析Go語言編程當中映射和方法的基本使用

淺析Go語言編程當中映射和方法的基本使用

熱門標簽:最短的地圖標注 騰訊外呼系統價格 谷歌便利店地圖標注 成都呼叫中心外呼系統平臺 浙江人工智能外呼管理系統 電梯外呼訪客系統 電銷機器人可以補救房產中介嗎 百度地圖標注搜索關鍵詞 ?兓?

映射
Go編程提供的一個重要的數據類型就是映射,唯一映射一個鍵到一個值。一個鍵要使用在以后檢索值的對象。給定的鍵和值,可以在一個Map對象存儲的值。值存儲后,您可以使用它的鍵檢索。

定義映射
必須使用make函數來創建一個映射。

復制代碼 代碼如下:

/* 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)


例子
下面的例子說明創建和映射的使用。

復制代碼 代碼如下:

package main

import "fmt"

func main {
   var coutryCapitalMap map[string]string
   /* create a map*/
   coutryCapitalMap = 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*/
   captial, 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")
   }
}


讓我們編譯和運行上面的程序,這將產生以下結果:

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() 函數
delete()函數是用于從映射中刪除一個項目。映射和相應的鍵將被刪除。下面是一個例子:

復制代碼 代碼如下:

package main

import "fmt"

func main {  
   /* create a map*/
   coutryCapitalMap := 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])
   }
}


讓我們編譯和運行上面的程序,這將產生以下結果:

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編程語言支持特殊類型的函數調用的方法。在方法聲明的語法中,“接收器”的存在是為了表示容器中的函數。該接收器可用于通過調用函數“.”運算符。下面是一個例子:

語法

復制代碼 代碼如下:

func (variable_name variable_data_type) function_name() [return_type]{
   /* function body*/
}
 package main

import (
   "fmt"
   "math"
)

/* define a circle */
type Circle strut {
   x,y,radius float64
}

/* define a method for circle */
func(circle Circle) area() float64 {
   return math.Pi * circle.radius * circle.radius
}

func main(){
   circle := Circle(x:0, y:0, radius:5)
   fmt.Printf("Circle area: %f", circle.area())
}


當上述代碼被編譯和執行時,它產生了以下結果:

Circle area: 78.539816

您可能感興趣的文章:
  • 深入解析Go語言中for循環的寫法
  • 簡介Go語言中的select語句的用法
  • 深入剖析Go語言編程中switch語句的使用

標簽:七臺河 眉山 宜昌 上海 邢臺 紹興 雅安 盤錦

巨人網絡通訊聲明:本文標題《淺析Go語言編程當中映射和方法的基本使用》,本文關鍵詞  淺析,語言編程,當中,映射,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《淺析Go語言編程當中映射和方法的基本使用》相關的同類信息!
  • 本頁收集關于淺析Go語言編程當中映射和方法的基本使用的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 岳普湖县| 郸城县| 崇义县| 大埔区| 安顺市| 栖霞市| 九江县| 平昌县| 三台县| 桃园县| 神木县| 南雄市| 景德镇市| 那曲县| 安徽省| 色达县| 通化市| 稻城县| 延津县| 沭阳县| 和林格尔县| 利川市| 普安县| 宜章县| 宁都县| 乐业县| 禹城市| 方正县| 凤凰县| 峡江县| 福贡县| 历史| 额敏县| 杭州市| 台湾省| 蚌埠市| 陕西省| 海宁市| 淮安市| 东港市| 安康市|