最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺析Go語言編程當(dāng)中映射和方法的基本使用

 更新時(shí)間:2015年10月30日 16:35:22   投稿:goldensun  
這篇文章主要介紹了淺析Go語言編程當(dāng)中映射和方法的基本使用,是golang入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下

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

定義映射
必須使用make函數(shù)來創(chuàng)建一個(gè)映射。

復(fù)制代碼 代碼如下:

/* 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)建和映射的使用。

復(fù)制代碼 代碼如下:

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


讓我們編譯和運(yùn)行上面的程序,這將產(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ù)是用于從映射中刪除一個(gè)項(xiàng)目。映射和相應(yīng)的鍵將被刪除。下面是一個(gè)例子:

復(fù)制代碼 代碼如下:

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])
   }
}


讓我們編譯和運(yùn)行上面的程序,這將產(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編程語言支持特殊類型的函數(shù)調(diào)用的方法。在方法聲明的語法中,“接收器”的存在是為了表示容器中的函數(shù)。該接收器可用于通過調(diào)用函數(shù)“.”運(yùn)算符。下面是一個(gè)例子:

語法

復(fù)制代碼 代碼如下:

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())
}


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

Circle area: 78.539816

相關(guān)文章

  • Golang中的泛型你真的了解嗎

    Golang中的泛型你真的了解嗎

    Golang?在?1.18?版本更新后引入了泛型,這是一個(gè)重要的更新,Gopher?萬眾矚目,為?Golang?帶來了更多的靈活性和可重用性,今天,我們將深入探討泛型的概念、為什么需要泛型、泛型的語法,并探討如何在實(shí)踐中使用它
    2023-05-05
  • Go語言死鎖與goroutine泄露問題的解決

    Go語言死鎖與goroutine泄露問題的解決

    最近在工作中使用golang編程,今天的文章給大家分享一下Go語言死鎖與goroutine泄露問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 從零封裝Gin框架實(shí)現(xiàn)日志初始化及切割歸檔功能

    從零封裝Gin框架實(shí)現(xiàn)日志初始化及切割歸檔功能

    這篇文章主要為大家介紹了從零封裝Gin框架實(shí)現(xiàn)日志初始化及切割歸檔功能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • Go語言中的IO操作及Flag包的用法

    Go語言中的IO操作及Flag包的用法

    這篇文章介紹了Go語言中的IO操作及Flag包的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • go語言中的return語句

    go語言中的return語句

    這篇文章主要介紹了go語言中的return語句,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)有所幫助
    2022-05-05
  • golang獲取prometheus數(shù)據(jù)(prometheus/client_golang包)

    golang獲取prometheus數(shù)據(jù)(prometheus/client_golang包)

    本文主要介紹了使用Go語言的prometheus/client_golang包來獲取Prometheus監(jiān)控?cái)?shù)據(jù),具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-03-03
  • GoLang BoltDB數(shù)據(jù)庫詳解

    GoLang BoltDB數(shù)據(jù)庫詳解

    這篇文章主要介紹了GoLang BoltDB數(shù)據(jù)庫,boltdb是使用Go語言編寫的開源的鍵值對數(shù)據(jù)庫,boltdb存儲(chǔ)數(shù)據(jù)時(shí) key和value都要求是字節(jié)數(shù)據(jù),此處需要使用到 序列化和反序列化
    2023-02-02
  • golang關(guān)閉chan通道的方法示例

    golang關(guān)閉chan通道的方法示例

    在go語言中,通道(channel)是一個(gè)非常重要的概念,通道提供了一種在不同 goroutine 之間安全地傳遞數(shù)據(jù)的方式,在本文中,我們將討論如何關(guān)閉通道以及在關(guān)閉通道時(shí)需要考慮的事項(xiàng),需要的朋友可以參考下
    2024-02-02
  • 一文詳解Golang連接kafka的基本操作

    一文詳解Golang連接kafka的基本操作

    這篇文章主要為大家詳細(xì)介紹了Golang中連接kafka的基本操作的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-03-03
  • Go get命令使用socket代理的方法

    Go get命令使用socket代理的方法

    由于某些不可描述的原因,國內(nèi)使用 go get 命令安裝某些包的時(shí)候會(huì)超時(shí)導(dǎo)致失敗,比如 net 包、 sys 包、 tools 包等。這篇文章給大家介紹go get 命令使用socket 代理的方法,感興趣的朋友一起看看吧
    2018-10-10

最新評(píng)論

桦川县| 盈江县| 依兰县| 五常市| 亚东县| 德安县| 凤城市| 白朗县| 虎林市| 仁寿县| 呼和浩特市| 石阡县| 沧源| 繁昌县| 西畴县| 富阳市| 临湘市| 榆树市| 监利县| 泸定县| 安多县| 无极县| 石狮市| 儋州市| 马山县| 明光市| 修武县| 灵石县| 固安县| 嘉荫县| 山西省| 普洱| 西吉县| 张掖市| 潞西市| 仁寿县| 孟津县| 花垣县| 宜兴市| 大宁县| 米林县|