• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >Golang > 浅析Go语言编程当中映射和方法的基本使用

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

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了网络语言特点浅析,浅析网络语言,幼儿语言教育活动浅析,浅析幼儿园语言教学,网络语言暴力浅析等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

</div>
例子
下面的例子说明创建和映射的使用。

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

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

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
</div>

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

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

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

</div>

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

</div>
当上述代码被编译和执行时,它产生了以下结果:

Circle area: 78.539816
</div>

</div>

您可能想查找下面的文章:

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

相关文章

  • Go语言基本的语法和内置数据类型初探
  • Go语言struct类型详解
  • 在Visual Studio Code中配置GO开发环境的详细教程
  • Go语言实现机器大小端判断代码分享
  • 在Mac OS上安装Go语言编译器的方法
  • GO语言实现的http抓包分析工具pproxy介绍
  • GO语言基本类型分析
  • go语言按显示长度截取字符串的方法
  • Go语言用map实现堆栈功能的方法
  • Go语言中使用gorm小结

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • GO语言实现文件上传代码分享
    • 使用os包和flag包实现读取main命令入参
    • GO语io包的常用接口
    • Go语言生成随机数的方法
    • GO语言映射(Map)用法分析
    • go语言里包的用法实例
    • go语言读取json并下载高清妹子图片
    • 谈谈Go语言的反射三定律
    • golang使用正则表达式解析网页
    • GO语言的IO方法实例小结

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有