• 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 通过本文主要向大家介绍了go语言函数,go语言,go语言环境搭建,go语言程序设计,let it go 25种语言等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

按值传递函数参数,是拷贝参数的实际值到函数的形式参数的方法调用。在这种情况下,参数在函数内变化对参数不会有影响。

默认情况下,Go编程语言使用调用通过值的方法来传递参数。在一般情况下,这意味着,在函数内码不能改变用来调用所述函数的参数。考虑函数swap()的定义如下。
/* function definition to swap the values */
func swap(int x, int y) int {
   var temp int

   temp = x /* save the value of x */
   x = y    /* put y into x */
   y = temp /* put temp into y */

   return temp;
}
</div>
现在,让我们通过使实际值作为在以下示例调用函数swap():
 package main

import "fmt"

func main() {
   /* local variable definition */
   var a int = 100
   var b int = 200

   fmt.Printf("Before swap, value of a : %d\n", a )
   fmt.Printf("Before swap, value of b : %d\n", b )

   /* calling a function to swap the values */
   swap(a, b)

   fmt.Printf("After swap, value of a : %d\n", a )
   fmt.Printf("After swap, value of b : %d\n", b )
}
func swap(x, y int) int {
   var temp int

   temp = x /* save the value of x */
   x = y    /* put y into x */
   y = temp /* put temp into y */

   return temp;
}
</div>
让我们把上面的代码放在一个C文件,编译并执行它,它会产生以下结果:

Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :100
After swap, value of b :200
</div>

这表明,参数值没有被改变,虽然它们已经在函数内部改变。

通过传递函数参数,即是拷贝参数的地址到形式参数的参考方法调用。在函数内部,地址是访问调用中使用的实际参数。这意味着,对参数的更改会影响传递的参数。

要通过引用传递的值,参数的指针被传递给函数就像任何其他的值。所以,相应的,需要声明函数的参数为指针类型如下面的函数swap(),它的交换两个整型变量的值指向它的参数。
/* function definition to swap the values */
func swap(x *int, y *int) {
   var temp int
   temp = *x    /* save the value at address x */
   *x = *y      /* put y into x */
   *y = temp    /* put temp into y */
}
</div>
现在,让我们调用函数swap()通过引用作为在下面的示例中传递数值:
package main

import "fmt"

func main() {
   /* local variable definition */
   var a int = 100
   var b int= 200

   fmt.Printf("Before swap, value of a : %d\n", a )
   fmt.Printf("Before swap, value of b : %d\n", b )

   /* calling a function to swap the values.
   * &a indicates pointer to a ie. address of variable a and
   * &b indicates pointer to b ie. address of variable b.
   */
   swap(&a, &b)

   fmt.Printf("After swap, value of a : %d\n", a )
   fmt.Printf("After swap, value of b : %d\n", b )
}

func swap(x *int, y *int) {
   var temp int
   temp = *x    /* save the value at address x */
   *x = *y    /* put y into x */
   *y = temp    /* put temp into y */
}
</div>
让我们把上面的代码放在一个C文件,编译并执行它,它会产生以下结果:

Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100
</div>

这表明变化的功能以及不同于通过值调用的外部体现的改变不能反映函数之外。

</div>

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

  • Go语言函数学习教程
  • Go语言中函数的参数传递与调用的基本方法
  • Go语言的os包中常用函数初步归纳
  • Go语言常见哈希函数的使用
  • go语言简单的处理http请求的函数实例
  • Go语言里的new函数用法分析
  • go语言版的ip2long函数实例
  • Go语言截取字符串函数用法
  • Go语言中普通函数与方法的区别分析
  • Go语言中append函数用法分析

相关文章

  • go语言中使用timer的常用方式
  • Go语言MessageBox用法实例
  • Golang极简入门教程(四):编写第一个项目
  • GO语言基本类型分析
  • go语言map字典删除操作的方法
  • Go语言实现socket实例
  • go语言template用法实例
  • GO语言基础之数组
  • Go语言使用sort包对任意类型元素的集合进行排序的方法
  • Go语言实现的树形结构数据比较算法实例

文章分类

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

最近更新的内容

    • Go语言hello world实例
    • go语言执行windows下命令行的方法
    • Go语言共享内存读写实例分析
    • Golang实现的聊天程序服务端和客户端代码分享
    • Go语言中的方法定义用法分析
    • Golang中channel使用的一些小技巧
    • Go语言截取字符串函数用法
    • Go语言中的指针运算实例分析
    • Go语言流程控制之goto语句与无限循环
    • Golang学习笔记(五):函数

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

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