Golang从入门到精通(十五):Golang指针
来源:互联网 发布:单页模板网站源码 编辑:程序博客网 时间:2024/06/04 19:57
指针概念
一个指针可以指向任何一个值的内存地址 它指向那个值的内存地址,在 32 位机器上占用 4 个字节,在 64 位机器上占用 8 个字节,并且与它所指向的值的大小无关。当然,可以声明指针指向任何类型的值来表明它的原始性或结构性;你可以在指针类型前面加上*
号(前缀)来获取指针所指向的内容,这里的*
号是一个类型更改器。使用一个指针引用一个值被称为间接引用。
Go语言指针
在Go语言中,直接砍掉了 C 语言指针最复杂的指针运算部分,只留下了获取指针(&
运算符)和获取对象(*
运算符)的运算,用法和C语言很类似。但不同的是,Go语言中没有->
操作符来调用指针所属的成员,而与一般对象一样,都是使用.
来调用。
Go 语言中一个指针被定义后没有分配到任何变量时,它的值为nil
。
Go 语言自带指针隐式解引用 :对于一些复杂类型的指针, 如果要访问成员变量时候需要写成类似*p.field
的形式时,只需要p.field
即可访问相应的成员。
new函数
使用 new 函数给一个新的结构体变量分配内存,它返回指向已分配内存的指针:
var t *T = new(T)
这条语句的惯用方法是:t := new(T)
,变量 t 是一个指向 T的指针,此时结构体字段的值是它们所属类型的零值。
注意:
用new(structName)
:这个方法得到的是*structName
类型,即类的指针类型;
用structName{init para}
:这个方法得到的是structName
类型,即类的实例类型,不是指针。
示例代码如下:
package mainimport ( "fmt")type Student struct { name string age int weight float32 score []int}func main(){ pp := new(Student) //使用 new 关键字创建一个指针 *pp = Student{"qishuangming", 23, 65.0, []int{2, 3, 6}} fmt.Printf("stu pp have %d subjects\n", len((*pp).score)) fmt.Printf("stu pp have %d subjects\n", len(pp.score)) //Go语言自带隐式解引用}
结构体和指针的综合应用
初始化一个结构体实例,除了上面讲的值类型初始化和结构体指针初始化,还有一种更简短和惯用的方式叫字面量初始化(结构体字面量:struct-literal
):
type struct1 struct { i1 int f1 float32 str string}ms := &struct1{10, 15.5, "Chris"} // 此时ms的类型是 *struct1
混合字面量语法(composite literal syntax
)&struct1{a, b, c}
是一种简写,底层仍然会调用new ()
,这里值的顺序必须按照字段顺序来写。在下面的例子中能看到可以通过在值的前面放上字段名来初始化字段的方式。所以归根结底,表达式 new(Type) 和 &Type{}
是等价的。
下面的例子显示了一个结构体Person
,一个方法,方法有一个类型为 *Person
的参数(因此对象本身是可以被改变的),以及三种调用这个方法的不同方式:
package mainimport ( "fmt" "strings")type Person struct { firstName string lastName string}func upPerson(p *Person) { p.firstName = strings.ToUpper(p.firstName) p.lastName = strings.ToUpper(p.lastName)}func main() { // 1-struct as a value type: var pers1 Person pers1.firstName = "Chris" pers1.lastName = "Woodward" upPerson(&pers1) fmt.Printf("The name of the person is %s %s\n", pers1.firstName, pers1.lastName) // 2—struct as a pointer: pers2 := new(Person) pers2.firstName = "Chris" pers2.lastName = "Woodward" (*pers2).lastName = "Woodward" // 这是合法的 upPerson(pers2) fmt.Printf("The name of the person is %s %s\n", pers2.firstName, pers2.lastName) // 3—struct as a literal: pers3 := &Person{"Chris","Woodward"} upPerson(pers3) fmt.Printf("The name of the person is %s %s\n", pers3.firstName, pers3.lastName)}//output/*The name of the person is CHRIS WOODWARDThe name of the person is CHRIS WOODWARDThe name of the person is CHRIS WOODWARD*/
- Golang从入门到精通(十五):Golang指针
- Golang从入门到精通(一):Golang介绍
- Golang从入门到精通(三):Golang数据类型
- Golang从入门到精通(八):Golang数组
- Golang从入门到精通(九):Golang字符串
- Golang从入门到精通(十):Golang函数
- Golang从入门到精通(十一):Golang方法
- Golang从入门到精通(十四):Golang结构体
- Golang从入门到精通(十六):Golang接口
- Golang从入门到精通(十七):Golang反射
- Golang从入门到精通(二):Golang开发环境配置
- Golang从入门到精通(四):Golang变量定义和初始化
- Golang从入门到精通(五):Golang控制语句之if/else
- Golang从入门到精通(六):Golang控制语句之for
- Golang从入门到精通(七):Golang控制语句之switch
- Golang从入门到精通(十二):Golang复合数据之slice
- Golang从入门到精通(十三):Golang复合数据之map
- Golang从入门到精通(十八):Golang并发编程之Goroutine
- 21、Android开发基础之了解AndroidManifest.xml清单文件
- 双重检查锁定(double-checked locking)与单例模式
- 如何在webpack中获取git版本和当前脚本的工作目录
- PHP mkdir()无写权限的问题解决方法
- leetcode 503. Next Greater Element II
- Golang从入门到精通(十五):Golang指针
- 利用html,JavaScript计算常用图形面积
- 外边距塌陷
- 从三大巨头产业生态圈分析物联网行业现状
- 内联函数机制解析
- 关于线程pthread_attr_t
- spring boot 集成 sitemesh
- 数据结构实验之查找五:平方之哈希表
- 17-CSS3 高级技巧 精灵图 滑动门