浅析Go语言编程当中映射和方法的基本使用

 更新时间:2015年10月30日 16:35:22   投稿:goldensun  
这篇文章主要介绍了浅析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")
   }
}


让我们编译和运行上面的程序,这将产生以下结果:

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

相关文章

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

    Golang中的泛型你真的了解吗

    Golang 在 1.18 版本更新后引入了泛型,这是一个重要的更新,Gopher 万众瞩目,为 Golang 带来了更多的灵活性和可重用性,今天,我们将深入探讨泛型的概念、为什么需要泛型、泛型的语法,并探讨如何在实践中使用它
    2023-05-05
  • Go语言死锁与goroutine泄露问题的解决

    Go语言死锁与goroutine泄露问题的解决

    最近在工作中使用golang编程,今天的文章给大家分享一下Go语言死锁与goroutine泄露问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • 从零封装Gin框架实现日志初始化及切割归档功能

    从零封装Gin框架实现日志初始化及切割归档功能

    这篇文章主要为大家介绍了从零封装Gin框架实现日志初始化及切割归档功能示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • Go语言中的IO操作及Flag包的用法

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

    这篇文章介绍了Go语言中的IO操作及Flag包的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • go语言中的return语句

    go语言中的return语句

    这篇文章主要介绍了go语言中的return语句,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下,希望对你的学习有所帮助
    2022-05-05
  • golang获取prometheus数据(prometheus/client_golang包)

    golang获取prometheus数据(prometheus/client_golang包)

    本文主要介绍了使用Go语言的prometheus/client_golang包来获取Prometheus监控数据,具有一定的参考价值,感兴趣的可以了解一下
    2025-03-03
  • GoLang BoltDB数据库详解

    GoLang BoltDB数据库详解

    这篇文章主要介绍了GoLang BoltDB数据库,boltdb是使用Go语言编写的开源的键值对数据库,boltdb存储数据时 key和value都要求是字节数据,此处需要使用到 序列化和反序列化
    2023-02-02
  • golang关闭chan通道的方法示例

    golang关闭chan通道的方法示例

    在go语言中,通道(channel)是一个非常重要的概念,通道提供了一种在不同 goroutine 之间安全地传递数据的方式,在本文中,我们将讨论如何关闭通道以及在关闭通道时需要考虑的事项,需要的朋友可以参考下
    2024-02-02
  • 一文详解Golang连接kafka的基本操作

    一文详解Golang连接kafka的基本操作

    这篇文章主要为大家详细介绍了Golang中连接kafka的基本操作的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-03-03
  • Go get命令使用socket代理的方法

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

    由于某些不可描述的原因,国内使用 go get 命令安装某些包的时候会超时导致失败,比如 net 包、 sys 包、 tools 包等。这篇文章给大家介绍go get 命令使用socket 代理的方法,感兴趣的朋友一起看看吧
    2018-10-10

最新评论