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()
}