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即分配又初始化内 ...
随机推荐
- 使用JS播放声音——SoundManager 2
<!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>SoundDemo& ...
- Jquery checkbox选中问题
checkbox中有.checked的写法,判断当前是否是选中状态,不过这种是针对[object HTMLInputElement]这种类型的,而对于[object Object]这种类型是不能使用的 ...
- centos7 elk install :ELK 安装 6.1.2版本
参考:http://blog.csdn.net/h952520296/article/details/78873849 (参考) 官网下载:https://www.elastic.co/cn/down ...
- 域名绑定和域名解析(DNS)有什么不同?(转载)
域名解析在DNS处设置,DNS服务器将你的域名指向你的存储网页的服务器. 域名绑定在服务器中设置,存储你网页文件的服务器绑定了你的域名才能把浏览者引导到这个域名指定的物理位置来访问. 比如,你进一个高 ...
- 使用as3crypto在Flex中实现AES加密
要在Flex中实现AES加密,可以通过as3crypto实现.但是as3crypto本身的用法比较复杂,一般是封装一下再调用. 下面是9RIA上的一篇文章给出的一个实现,使用中稍感不方便(见注释): ...
- 百度地图api定位和导航简写
function locate() { // 百度地图API功能 var map = new BMap.Map("allmap"); // 创建Map实例 var point = ...
- Thinkphp上传图片
上传图片的HTML结构: <form action="{:U('Config/addImg')}" enctype="multipart/form-data&quo ...
- log4j.properties配置详解与实例(转载)
转自:http://blog.sina.com.cn/s/blog_5ed94d710101go3u.html 最近使用log4j写log时候发现网上的写的都是千篇一律,写的好的嘛不全,写的全一点的嘛 ...
- couldn't connect to host
“couldn't connect to host” 这样的错误可能是主机不可到达,或者端口不可到达. ping OK只代表主机可以到达. 端口不可到达可能是由于HTTP 服务器未启动或者监听在其他端 ...
- 热词统计以及Quartz.net的简单使用
一.热词统计 方案一: 设计一个表:ID KeyWord Count 当用户再输入框中查询的时候,我们就往表中插入数据,在插入之前首先判断是否已经存在keyword,存在的话,让C ...