go的指针类型,必须初始化后才能赋值
package main
import(
"fmt"
)
func testPoint5(){
var a *int = new(int) // 用new初始化,就可以直接赋值了 如果直接用var a *int 之后直接赋值,就报错
*a = 100
fmt.Println("---------------*a = 100-------------------")
fmt.Printf("*a=%d\n",*a)
fmt.Printf("地址a=%p\n",a)
var b *[]int =new([]int)
fmt.Printf("*b=%v\n",*b)
// *b[0] =100 // b[0] (type *[]int does not support indexing) 用new方法创建的空数组不能直接赋值
(*b) = make([]int, 5,100)
(*b)[0] =100
(*b)[1] =200
fmt.Printf("*b=%v\n",*b)
}
func testPoint6(){
var a int = 100
b := a
fmt.Println("---------------b := a = 100-------------------")
fmt.Printf("a=%d\n",a)
fmt.Printf("b=%d\n",b)
fmt.Printf("&a=%p\n",&a)
fmt.Printf("&b=%p\n",&b)
fmt.Println("--------------- a = 120-------------------")
a = 120
fmt.Printf("a=%d\n",a)
fmt.Printf("b=%d\n",b)
fmt.Printf("&a=%p\n",&a)
fmt.Printf("&b=%p\n",&b)
fmt.Println("--------------- a = 130-------------------")
b= 130
fmt.Printf("a=%d\n",a)
fmt.Printf("b=%d\n",b)
fmt.Printf("&a=%p\n",&a)
fmt.Printf("&b=%p\n",&b)
}
func testPoint7(){
// 只有内存地址一样,值怎么变都是一样的
var a int = 100
var b *int = &a
var c *int = b
fmt.Println("---------------a int = 100-------------------")
fmt.Printf("地址*a=%d,实际值a=%d\n",&a,a)
fmt.Printf("地址b=%d, 实际值*b=%d\n",b,*b)
fmt.Printf("地址c=%d, 实际值*c=%d\n",c,*c)
fmt.Println("---------------*c = 200-------------------")
*c = 200
fmt.Printf("实际值a=%d, 地址*a=%d\n",a,&a)
fmt.Printf("地址b=%d, 实际值*b=%d\n",b,*b)
fmt.Printf("地址c=%d, 实际值*c=%d\n",c,*c)
fmt.Println("---------------a =120-------------------")
a = 120
fmt.Printf("a=%d, *a=%d\n",a,&a)
fmt.Printf("b=%d, *b=%d\n",b,*b)
fmt.Printf("c=%d, *c=%d\n",c,*c)
fmt.Println("---------------*b = 130-------------------")
*b = 130
fmt.Printf("a=%d, *a=%d\n",a,a)
fmt.Printf("b=%d, *b=%d\n",b,*b)
fmt.Printf("c=%d, *c=%d\n",c,*c)
}
func modify(a *int){
*a =105
}
func testPoint8(){
var b int = 100
p := &b
fmt.Printf("修改之前的b=%d\n",b)
fmt.Printf("修改之前的p=%d\n",*p)
modify(p)
fmt.Printf("修改之后的b=%d\n",b)
}
func modify_arr(a *[3]int){
(*a)[0] =105
}
func testPoint4(){
var b [3]int = [3]int{1,2,3}
fmt.Printf("b=%v\n",b)
modify_arr(&b)
fmt.Printf("b=%v\n",b)
}
func testPoint9(){
var a *int = new(int)
*a = 120
fmt.Printf("a=%v a=%d\n",a,*a)
var b *[]int = new([]int)
fmt.Printf("b=%v\n",*b)
(*b) = make([]int, 5, 100) //初始化切片 也可以简写成 *b = make([]int, 5,100)
(*b)[0] = 100
(*b)[2] = 105
fmt.Printf("b=%v\n",*b)
}
func modifyInt(a int){
print("00>>",a,"\n")
a = 162
print("01>>",a,"\n")
}
func testPoint3()(){
var a int32
a = 1000
fmt.Printf("the addr of a :%p, a:%d a:%v\n",&a,a,a)
var b *int32
fmt.Printf("the addr of b:%p b:%p b:%v\n",&b,b,b)
// *b =252 // 指针类型必须先初始化才能赋值,否则报错
if b==nil{
fmt.Println("b is nil addr")
}
b = &a
fmt.Printf("the addr of b:%p,b:%p b:%v\n",&b,b,b)
fmt.Printf("the addr of b:%p,b:%d\n",&b,*b)
fmt.Println("--------------- *b =250-------------------")
*b =250
fmt.Printf("the addr of b:%p,b:%d\n",&b,*b)
fmt.Printf("the addr of a :%p, a:%d a:%d\n",&a,a,a)
}
func main(){
testPoint3()
testPoint4()
testPoint5()
testPoint6()
testPoint7()
testPoint8()
testPoint9()
}
输出:
the addr of a :0xc00019c058, a:1000 a:1000
the addr of b:0xc0001c6020 b:0x0 b:<nil>
b is nil addr
the addr of b:0xc0001c6020,b:0xc00019c058 b:0xc00019c058
the addr of b:0xc0001c6020,b:1000
--------------- *b =250-------------------
the addr of b:0xc0001c6020,b:250
the addr of a :0xc00019c058, a:250 a:250
b=[1 2 3]
b=[105 2 3]
---------------*a = 100-------------------
*a=100
地址a=0xc00019c078
*b=[]
*b=[100 200 0 0 0]
---------------b := a = 100-------------------
a=100
b=100
&a=0xc00019c0e8
&b=0xc00019c0f0
--------------- a = 120-------------------
a=120
b=100
&a=0xc00019c0e8
&b=0xc00019c0f0
--------------- a = 130-------------------
a=120
b=130
&a=0xc00019c0e8
&b=0xc00019c0f0
---------------a int = 100-------------------
地址*a=824635408632,实际值a=100
地址b=824635408632, 实际值*b=100
地址c=824635408632, 实际值*c=100
---------------*c = 200-------------------
实际值a=200, 地址*a=824635408632
地址b=824635408632, 实际值*b=200
地址c=824635408632, 实际值*c=200
---------------a =120-------------------
a=120, *a=824635408632
b=824635408632, *b=120
c=824635408632, *c=120
---------------*b = 130-------------------
a=130, *a=130
b=824635408632, *b=130
c=824635408632, *c=130
修改之前的b=100
修改之前的p=100
修改之后的b=105
a=0xc00019c160 a=120
b=[]
b=[100 0 105 0 0]
写入自己的博客中才能记得长久