淺析Go語言編程當(dāng)中映射和方法的基本使用
映射
Go編程提供的一個(gè)重要的數(shù)據(jù)類型就是映射,唯一映射一個(gè)鍵到一個(gè)值。一個(gè)鍵要使用在以后檢索值的對(duì)象。給定的鍵和值,可以在一個(gè)Map對(duì)象存儲(chǔ)的值。值存儲(chǔ)后,您可以使用它的鍵檢索。
定義映射
必須使用make函數(shù)來創(chuàng)建一個(gè)映射。
/* 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")
}
}
讓我們編譯和運(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è)例子:
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è)例子:
語法
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)文章
Go Time庫中時(shí)間和日期相關(guān)的操作方法整理
這篇文章主要為大家整理了Go語言中的time庫,包括時(shí)間、日期和時(shí)區(qū)等相關(guān)概念及使用方法,希望通過掌握這些知識(shí),大家可以更好地處理時(shí)間、日期和時(shí)區(qū)相關(guān)的問題2023-08-08Go語言開發(fā)kube-scheduler整體架構(gòu)深度剖析
這篇文章主要為大家介紹了Go語言開發(fā)kube-scheduler整體架構(gòu)深度剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Go channel發(fā)送方和接收方如何相互阻塞等待源碼解讀
這篇文章主要為大家介紹了Go channel發(fā)送方和接收方如何相互阻塞等待源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Go語言實(shí)現(xiàn)二進(jìn)制與十進(jìn)制互轉(zhuǎn)的示例代碼
這篇文章主要和大家詳細(xì)介紹了Go語言中實(shí)現(xiàn)二進(jìn)制與十進(jìn)制互相轉(zhuǎn)換的示例代碼,文中的代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05Golang實(shí)現(xiàn)單元測(cè)試中的邏輯層
前面我們完成了最麻煩的數(shù)據(jù)層的單元測(cè)試,今天我們來看看單元測(cè)試中最容易做的一層,數(shù)據(jù)邏輯層,也就是我們通常說的 service 或者 biz 等2023-03-03