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即分配又初始化内 ...
随机推荐
- JSP自定义标签rtexprvalue属性
rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否可以使用JSP表达式.(比如EL表达式或OGNL表达式). 当在<attribute>标 ...
- mount: block device /dev/cdrom is write-protected, mounting read-only 解决方法
[root@localhost ~]# mount /dev/cdrom /mnt/cdrom/ mount: block device /dev/sr0 is write-protected, mo ...
- php实现一个简单的购物网站
实现一个简单的购物网站 一.考试时间:8小时 二.开发工具:DW 三.数据库:见附件 四.需要实现的页面: Index:浏览商品页面,显示商品列表,用户可以点击“购买“. ViewCart:查看购物车 ...
- 《转》python学习(4)对象
转自http://www.cnblogs.com/BeginMan/p/3160044.html 一.学习目录 1.pyhton对象 2.python类型 3.类型操作符与内建函数 4.类型工厂函数 ...
- Go基础---->go的基础学习(一)
这里面记录一些学习go的基础知识.我希望爱我的人不寂寞,我希望我爱的人喜欢我 go的基础知识 一.go中的map的使用 package main import ( "fmt" ) ...
- with操作符损耗性能的原因
当函数运行是,创建一个[[scope]]指向的被称为作用域链的可变对象集合,作用域链的最前端是一个包含所有的局部变量.参数.this等的被称为“激活对象”的对象. 在标示符查找的过程中,从作用域的最前 ...
- 自行颁发不受浏览器信任的SSL证书
ssh登陆到服务器上,终端输入以下命令,使用openssl生成RSA密钥及证书. # 生成一个RSA密钥 $ openssl genrsa -des3 -out 33iq.key 1024 # 拷贝一 ...
- HTML的特殊字符-图标对应表
本文摘自:http://www.cnblogs.com/web-d/archive/2010/04/16/1713298.html HTML特殊字符编码大全:往网页中输入特殊字符,需在html代码 ...
- EUI Scroller实现自定义图片轮播 组件ScrollView
一 自定义组件如下 /** * 文 件 ScrollView.ts * 功 能: 滚动组件 * 内 容: 自定义组件,支持多张图片水平(垂直)切换滚动 * * Example: * 1. 从自定义组件 ...
- SSH电力项目二
底层方法封装(CommonDaoImpl类) public class CommonDaoImpl<T> extends HibernateDaoSupport implements IC ...