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 */
}
让我们把上面的代码放在一个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
这表明变化的功能以及不同于通过值调用的外部体现的改变不能反映函数之外 。
go语言数组和指针的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于go语言数组和指针的区别、go语言数组和指针的信息别忘了在本站进行查找喔 。
- mysql游标和存储过程是什么 mysql游标表名为变量
- mysql子查询和连接查询 mysql子查询插入
- 纯phpmysql
- mongodb存储图片和文件实践 mongodb存文件和表
- java查询数组中是否包含某一个值 javamongodb数组查询
- 数据库和redis数据不一致 h2数据库和redis
- mongodb 权威指南 mongodb权威指南和实战
- mongo 新建数据库 mongodb创建用户和数据库
- redis怎么和数据库交互 redis数据结合
- mongodb怎么查看数据 mongodb查看用户名和密码
