a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > Golang > 正文

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

2020-04-01 19:12:20
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了淺析Go語言編程當中映射和方法的基本使用,是golang入門學習中的基礎知識,需要的朋友可以參考下
 

映射
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") 
   }
}

 


讓我們編譯和運行上面的程序,這將產生以下結果:
  1. Capital of India is New Delhi 
  2. Capital of France is Paris 
  3. Capital of Italy is Rome 
  4. Capital of Japan is Tokyo 
  5. 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])
   }
}

 


讓我們編譯和運行上面的程序,這將產生以下結果:
  1. Original Map 
  2. Capital of France is Paris 
  3. Capital of Italy is Rome 
  4. Capital of Japan is Tokyo 
  5. Capital of India is New Delhi 
  6. Entry for France is deleted 
  7. Updated Map 
  8. Capital of India is New Delhi 
  9. Capital of Italy is Rome 
  10. 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())
}

 


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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品极品美女在线观看免费 | 蜜桃久久| 日韩和欧美的一区二区 | 色av色av色av | 久久精品欧美 | 亚洲毛片| 青青青草视频 | 91高清在线 | 亚洲精品一区二三区不卡 | 国产一区二区三区久久久久久久久 | 男人的天堂免费 | 久久国产精品视频 | 欧美日韩精品一区二区 | 99久久婷婷国产综合精品 | 日韩欧美久久 | 精品一区二区三区免费 | 成人激情在线 | 国产精品久久久久久久久久新婚 | 欧美日本在线观看 | 日本免费一区二区三区 | 日韩精品在线观看视频 | 亚洲v欧美 | 日韩视频精品 | 日本三级黄色录像 | 在线视频 中文字幕 | 欧美激情一区二区三区四区 | 国产精品亚洲成人 | 午夜精品久久久久 | 亚洲国产精品成人无久久精品 | 亚洲欧美激情在线 | 久久福利| 亚洲精品久久久日韩美女极品合集下载 | 超碰免费观看 | 免费99精品国产自在在线 | 国产精品一区二区三 | 久久亚洲精品国产一区最新章节 | 国产亚洲一区二区三区在线 | h色网站免费观看 | 成人国产在线观看 | 国产在线不卡观看 | 一区中文字幕 |