Golang 中的指针 - Pointer
func passValue(s struct){} func passPointer(s *struct){}
package main import "fmt" type MyPoint struct {
X int
Y int
} func printFuncValue(p MyPoint){
p.X = 1
p.Y = 1
fmt.Printf(" -> %v", p)
} func printFuncPointer(pp *MyPoint){
pp.X = 1 // 实际上应该写做 (*pp).X,Golang 给了语法糖,减少了麻烦,但是也导致了 * 的不一致
pp.Y = 1
fmt.Printf(" -> %v", pp)
} func (p MyPoint) printMethodValue(){
p.X += 1
p.Y += 1
fmt.Printf(" -> %v", p)
} // 建议使用指针作为方法(method:printMethodPointer)的接收者(receiver:*MyPoint),一是可以修改接收者的值,二是可以避免大对象的复制
func (pp *MyPoint) printMethodPointer(){
pp.X += 1
pp.Y += 1
fmt.Printf(" -> %v", pp)
} func main(){
p := MyPoint{0, 0}
pp := &MyPoint{0, 0} fmt.Printf("\n value to func(value): %v", p)
printFuncValue(p)
fmt.Printf(" --> %v", p)
// Output: value to func(value): {0 0} -> {1 1} --> {0 0} //printFuncValue(pp) // cannot use pp (type *MyPoint) as type MyPoint in argument to printFuncValue //printFuncPointer(p) // cannot use p (type MyPoint) as type *MyPoint in argument to printFuncPointer fmt.Printf("\n pointer to func(pointer): %v", pp)
printFuncPointer(pp)
fmt.Printf(" --> %v", pp)
// Output: pointer to func(pointer): &{0 0} -> &{1 1} --> &{1 1} fmt.Printf("\n value to method(value): %v", p)
p.printMethodValue()
fmt.Printf(" --> %v", p)
// Output: value to method(value): {0 0} -> {1 1} --> {0 0} fmt.Printf("\n value to method(pointer): %v", p)
p.printMethodPointer()
fmt.Printf(" --> %v", p)
// Output: value to method(pointer): {0 0} -> &{1 1} --> {1 1} fmt.Printf("\n pointer to method(value): %v", pp)
pp.printMethodValue()
fmt.Printf(" --> %v", pp)
// Output: pointer to method(value): &{1 1} -> {2 2} --> &{1 1} fmt.Printf("\n pointer to method(pointer): %v", pp)
pp.printMethodPointer()
fmt.Printf(" --> %v", pp)
// Output: pointer to method(pointer): &{1 1} -> &{2 2} --> &{2 2}
}
map["name"]="Jason";
而如果使用 map 的指针,反而会产生错误:
*map["name"]="Jason" // invalid indirect of m["title"] (type string)
(*map)["name"]="Jason" // invalid indirect of m (type map[string]string)
Golang 中的指针 - Pointer的更多相关文章
- golang 中的指针
# golang 中的指针 看了一篇[文章](http://blog.51cto.com/steed/2341409),写的很好.这里略微总结下重点: 1. 地址值.unsafe.Pointer.ui ...
- golang中数组指针与指针数组的区别实现
指针数组和数组的指针,指的是两个不同的东西. 指针数组是有指针组成的数组,数组的指针是一个数组的指针. package main import "fmt" const MAX ...
- golang中数组指针和指针数组的区别
func test(){ x,y := 1, 2 var arr = [...]int{5:2} //数组指针 var pf *[6]int = &arr //指针数组 pfArr := [. ...
- golang中数组指针和指针数组当做函数参数如何修改数组中的值
先理解:数组指针它的类型时指针,指针数组它的类型时数组 1. 数组指针当做函数的参数 package main import "fmt" func changeData(dataA ...
- 说说不知道的Golang中参数传递
本文由云+社区发表 导言 几乎每一个C++开发人员,都被面试过有关于函数参数是值传递还是引用传递的问题,其实不止于C++,任何一个语言中,我们都需要关心函数在参数传递时的行为.在golang中存在着m ...
- 【荐】详解 golang 中的 interface 和 nil
golang 的 nil 在概念上和其它语言的 null.None.nil.NULL一样,都指代零值或空值.nil 是预先说明的标识符,也即通常意义上的关键字.在 golang 中,nil 只能赋值给 ...
- 第十九课 golang中的下划线
在 Golang 里, _ (下划线)是个特殊的标识符. 用在 import 在导包的时候,常见这个用法: 1 2 import _ "net/http/pprof" import ...
- 在Golang中使用C语言代码实例
转自:http://www.jb51.net/article/56720.htm cgo 使得在 Golang 中可以使用 C 代码. Hello World 为了有一个较为直观的了解,我们来看一个简 ...
- golang中,new和make的区别
在golang中,make和new都是分配内存的,但是它们之间还是有些区别的,只有理解了它们之间的不同,才能在合适的场合使用. 简单来说,new只是分配内存,不初始化内存: 而make即分配又初始化内 ...
随机推荐
- PyQt4重写事件处理方法
PyQt中的事件处理主要以来重写事件处理函数来实现. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import Qt ...
- JS-随机div颜色
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【黑金ZYNQ7000系列原创视频教程】03.体验FPGA里的ARM——裸机helloworld实验
黑金论坛地址: http://www.heijin.org/forum.php?mod=viewthread&tid=36637&extra=page%3D1 爱奇艺地址: http: ...
- android 使用动画 Button移动后不响应点击事件的解决办法
animation3.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimatio ...
- OC开发_Storyboard——AutoLayout
一.autolayout 自动布局: 1. 设置所有视图框架的三种方法,可以通过代码创建也可以storyboard设置 = 规则 (1 蓝线+约束:(位置) 使用蓝线,根据蓝线拖动控件,只是告诉Xco ...
- 报警告session_regenerate_id(): Failed to create(read) session ID: files (path: N;/path)
php.ini文件中的session.save_path = "N;/path"注释掉(前面加分号)
- python获取当天日期进行格式转换
# Python Library import time def getToday(format=3): """返回今天的日期字串""" # ...
- top与with ties用法
使用top中把与最后一条记录值相同的数据也放入列表中 一.SQL SERVER中使用WITH TIES的用途 with ties一般是和Top , order by相结合使用的,会查询出最后一条数据额 ...
- python __init__ 构造函数
实例化过程 会执行__init__ 的函数方法 class SQLHelper: def __init__(self): # self = s1 print("helo") def ...
- PID参数调整的口诀
PID参数调整的口诀:参数整定找最佳,从小到大顺序查先是比例后积分,最后再把微分加曲线振荡很频繁,比例度盘要放大曲线漂浮绕大湾,比例度盘往小扳曲线偏离回复慢,积分时间往下降曲线波动周期长,积分时间再加 ...