博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Golang之指针(point)再探索
阅读量:4352 次
发布时间:2019-06-07

本文共 1439 字,大约阅读时间需要 4 分钟。

先记录代码

package mainimport "fmt"/*声明指针*T 是指向类型T的值的指针变量的类型*/func main() {    //b := 255    //var a *int = &b // a是int指针,指向b的内存地址    //fmt.Printf("Type of is:%T\n", a)    //fmt.Println("address of b is", a)    //    //a := 22    //var b *int //b 这个变量是int类型的指针变量,变量的值,也只能是int型指针    //if b == nil {    //    // 指针的零值是nil    //    fmt.Println("b is", b)    //    b = &a    //    fmt.Println("b after initialization is", b)    //}    //b := 255    //a := &b    //fmt.Println("address of b is", a)//打印b的内存地址    //fmt.Println("value of b is", *a)//打印b的值,可以通过*a指针    //b := 255    //a := &b    //fmt.Println("address of b is:", a) //b的内存地址    //fmt.Println("value of b is:", *a)    //*a++ //通过a的指针加一    //fmt.Println("new value of b is:", b)    //a := 58    //fmt.Println("value of a befor func call is:", a)    //b := &a    //change(b) //指针变量b,改变a的值,a=55,    //fmt.Println("value of a after call is:", a)    /*        不要将指向数组的指针,作为参数传递给函数,改用切片    */    //a := [3]int{89, 90, 91}    //modify(&a) //传递数组a的地址,给modify    //fmt.Println(a)    a := [3]int{
89, 90, 91} modify(a[:]) //传入一个a的切片 fmt.Println(a)}//函数传递指针,改变参数的内存地址。//func change(val *int) {// *val = 55//修改数组的值//传递指向数组的指针,作为参数,并且对其修改//func modify(arr *[3]int) {// (*arr)[0] = 90// //arr[0]=90 //也可以这么写,这是上面的简写形式(*a)[X]可以写成a[X]//}//切片方式修改函数//这个方法是修改函数最常用的,最好的方法。。。。。。。。。。。。func modify(sls []int) { sls[0] = 91}//Go不支持如同C的指针运算

 

转载于:https://www.cnblogs.com/pyyu/p/8269342.html

你可能感兴趣的文章
mysql数据库的增删改
查看>>
Vue中使用axios如何解决跨域问题
查看>>
Eclipse中导入外部jar包步骤
查看>>
基本控件文档-UIButton属性
查看>>
poj1065
查看>>
(C#) VS类视图和对象浏览器图标
查看>>
SPOJ 1811 LCS [后缀自动机]
查看>>
BZOJ 3527: [Zjoi2014]力 [快速傅里叶变换]
查看>>
LeetCode Palindrome Permutation II
查看>>
LeetCode Minimum Index Sum of Two Lists
查看>>
linux学习系列三
查看>>
细看Thread的 start() 和 run()方法
查看>>
Maven项目无法添加到tomcat
查看>>
查看公司工商注册信息
查看>>
小tip: 使用SVG寥寥数行实现圆环loading进度效果
查看>>
科技发展潮流
查看>>
Reactor-反应器模式
查看>>
Object的wait/notify/notifyAll&&Thread的sleep/yield/join/holdsLock
查看>>
MVC3+EntityFramework实践笔记
查看>>
一个漂亮的 PlaceHolder
查看>>