go指针类型

*** 对指针类型,取值, 对数值类型&取址**

点击查看代码
package main

import(
	"fmt"		
)



func modify(a *int){
	*a  = 100  // 对指针类型,*取值, 对数值类型&取址
}

func testInt2(){
	var a int = 10
	fmt.Printf("before modify %d addr:%p\n",a,&a)
	modify(&a)
	fmt.Printf("after modify %d addr:%p\n",a,&a)
}


func swap(a int, b int){
	fmt.Printf("00 a=%d b=%d\n",a,b)
	a,b = b,a
	fmt.Printf("01  a=%d b=%d\n",a,b)
}

func testInt3(){
	var a int = 10
	var b int = 20
	fmt.Printf("testInt3 01: a=%d b=%d\n",a,b)
	swap(a,b)
	fmt.Printf("testInt3 02: a=%d b=%d\n",a,b)
}


func swap_addr(a *int, b *int){  // 对指针类型,*取值, 对数值类型&取址
	fmt.Printf("00 &a=%d &b=%d\n",a,b)
	*a,*b = *b,*a
	fmt.Printf("01  &a=%d &b=%d\n",a,b)
}

func testInt4(){
	var a int = 10
	var b int = 20
	fmt.Printf("testInt3 01: a=%d b=%d\n",a,b)
	swap_addr(&a,&b)
	fmt.Printf("testInt3 02: a=%d b=%d\n",a,b)
}


// 对指针类型,*取值, 对数值类型&取址
func main(){	
	testInt2()
	//testInt3()
	//testInt4()
}
posted @ 2022-03-05 23:50  ty1539  阅读(19)  评论(0编辑  收藏  举报