溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2021-09-17 15:49:20 來源:億速云 閱讀:113 作者:柒染 欄目:編程語言

本篇文章為大家展示了Go語言編程當中映射和方法的基本使用方法,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

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

定義映射
必須使用make函數來創(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 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())
}


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

Circle area: 78.539816

上述內容就是Go語言編程當中映射和方法的基本使用方法,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

go
AI