GO complex language Type 01 --- pointer

package main

/*
% T type placeholder
% V value placeholder
% P addresses (pointers) placeholder to replace only the address% p
& Value address-value
* Addr address value
** int int type pointer points to a pointer
Effect: when the function parameter passing, called the reference pointer passed passed passed value type is copied, is transmitted to the modification will not change the parameters of the body, the body wants to change only passed by reference
*/

import "fmt"

// pointer is the address
// & value Taking the address of a value
// * ptr address value
main021 function () {

	When // declare variables a, the system opened up a memory [address], which kept the value of [123]
	var int = 123
	fmt.Printf ( "a type is% T \ n", a) // int
	fmt.Printf ( "the value of a is% v \ n", a) // 123
	fmt.Printf ( "a address is% p \ n", & a) // 0x ...

	// & a fetch address of a variable
	aPointer := &a
	fmt.Printf ( "type aPointer is% T \ n", aPointer) // * int

	// The value of the address aPointer pointed revised to 456
	*aPointer = 456
	fmt.Println("*aPointer=",*aPointer)//456
	// the value of a becomes 456
	fmt.Println("a=",a)
}

// define the original variable, modify the value of the pointer which will directly change the original variables
// pointer if there is no re-assignment, always reference the same address
main022 function () {

	// x system to allocate memory
	var x = 456
	fmt.Println(x)//456

	// define integer pointer (address storing integer data)
	// default assignment is empty <nil>
	was xPtr * int
	fmt.Println("xPtr=", xPtr)//nil

	// x is pointing to address aPtr
	xPtr = &x

	// The value of the address xPtr pointed revised to 789
	*xPtr = 789

	fmt.Println(x)         //789
	fmt.Println(xPtr)      //0xc042052080
	fmt.Println(&x)        //0xc042052080
	fmt.Println(*xPtr)     //789
	fmt.Println(*xPtr == x) //true
	fmt.Println(xPtr == &x) //true

	var y = 456
	* XPtr = y
	fmt.Println(x)          //456
	fmt.Println (xPtr) // not changed
	fmt.Println (& x) // not changed
	fmt.Println(*xPtr)      //456
	fmt.Println(*xPtr == y) //true
	fmt.Println(x == y)     //true
	fmt.Println(&x == &y)   //false
	fmt.Println(xPtr == &y) //false
	fmt.Printf ( "y address is% p \ n", & y)

	fmt.Println("----------")
	// xPtr point to the address of y
	xPtr = &y
	y = 789
	fmt.Println(x)      //456
	fmt.Println (y) // 789
	fmt.Println(*xPtr)	//789
	fmt.Println(&x == xPtr) //false
}

// pointer detection data type is strictly
main023 function () {
	var x = 123
	var intPtr *int
	intPtr = &x
	fmt.Println(intPtr, *intPtr) //0x...,123

	// var y = "your sister"
	// intPtr = & y // compile error, [] can not be assigned an integer pointer to a string [address]
}

// pointer to pointer
main024 function () {
	var x = 123
	var mPtr *int = &x

	** // var MMP you
	mmPtr := &mPtr
	fmt.Println(mmPtr)//0xc042072018
	fmt.Printf ( "type is mmPtr% T \ n", mmPtr) // ** int type is mmPtr

	// print the value of x
	fmt.Println(*mPtr)//123
	fmt.Println(*(*mmPtr))//123
	fmt.Println(**mmPtr)//123
}


func main() {
	//main021()
	//main022()
	// main023 ()
	main024()
}

  

Guess you like

Origin www.cnblogs.com/yunweiqiang/p/11809002.html