What is a pointer

When writing code, we use variables to manipulate values stored in memory, perform assignments, add and subtract operations, and so on. Have you ever wondered what the variable represents? In fact, a variable corresponds to a segment of memory space, which stores the value of the corresponding type of the variable. The value of the pointer corresponds to the address of the variable, and the value of the variable can be updated or read using the pointer alone, without using the variable name.

var i int = 10      // declare variable I and initialize to 10
var ptr *int = &i 
fmt.Println(ptr,*ptr)
// 0xC000018060 10 PTR stores the I address, * PTR corresponds to the value of the variable to which the pointer points
*ptr = 12  				// I =12 updates the value of the variable pointed to by the pointer
fmt.Println(*ptr,i)		/ / 12 of 12
Copy the code

This code declares a pointer variable of type *int PTR, using the address operator & to get the address to int I. We can say that the pointer PTR points to variable I, or that the PTR pointer holds the address of variable I.

Pointer to the statement

Pointers refer to the memory address of a variable. To use a pointer, you must declare it in the following format:

var var_name *var_type
Var_name Specifies the name of the pointer. Var_type specifies the type of the variable to which the pointer points
Copy the code
var i int = 10
str := "go"
var ip *int         // * a pointer of type int
var pstr *string    // * A pointer to string
ip = &i
pstr = &str
fmt.Println(*ip,*pstr)     // 10 go
Copy the code

*int pointer to a variable of type int; *string pointer to a variable of type int.

str := "go"
var ip *int
ip = &str  // The compile will not pass
Copy the code

* If a pointer to an int is a string, the compiler will raise an error:

cannot use &str (type *string) as type *int in assignment
Copy the code

How to use Pointers

How to use Pointers is already listed above. There are three main steps: declare, assign, and access the value of the variable to which the pointer points

x,y := 1."go"
var px *int = &x    	// 1. Declare and initialize
var py = &y				// 2, omit pointer type, compiler automatically judge
//px,py := &x,&y //
fmt.Println(*px,*py) 	// 1 go access
Copy the code

About null Pointers

When a pointer is declared but not assigned, it is called null and is nil. The zero value of any type of pointer is nil.

var ip *int
fmt.Println(ip)							// nil
fmt.Printf("The value of IP is :%x", ip)			// The value of IP is 0
Copy the code

If IP! = nil is true, so p is pointing to some valid variable. Pointers can also be tested for equality, only if they point to the same variable or if they are all nil.

1, pointing to the same variabletrue
x,_ := 1.1
px,py := &x,&x
fmt.Println(px == py) 
 
2, pointing to different variablesfalse
x,y := 1.1
px,py := &x,&y
fmt.Println(px == py)

3, twonilPointer to thetrue
var px *int
var py *int
fmt.Println(px == py)
Copy the code

Pointers are used as function arguments

A pointer contains the address of a variable. If you pass a pointer as an argument to a function, you can update the value of the variable through the pointer.

func a(p *int){
	*p++
}

i := 10
fmt.Println(i)      / / 10
a(&i);
fmt.Println(i)		/ / 11
Copy the code

New functions that are not commonly used

New (type) creates an anonymous variable of type type, initializes it to a zero value of type, returns the address of the variable, and returns a pointer of type *type.

p := new(int)   	// p, *int, pointing to an anonymous int variable
fmt.Println(*p) 	/ / 0
*p = 2          	// Set the int anonymous variable to 2
fmt.Println(*p) 	/ / 2
Copy the code

Creating a variable using the new function is no different from creating a variable using a normal variable declaration statement, except that you do not need to declare the name of a temporary variable. The following two functions have the same behavior: create a variable and return the address of the variable

func newA(a) *int {
    return new(int)}func newB(a) *int {
    var i int
    return &i
}
Copy the code

Each call to new returns the address of the new variable:

p := new(int)
q := new(int)
fmt.Println(p,q)   // 0xc000018060 0xc000018068
Copy the code

New function is used less, but feel quite interesting, in this to share with you! In practical programming, it is more convenient to create variables using concrete types, right?


Original article, if need to be reproduced, please indicate the source! Check out “Golang is coming” or go to seekload.net for more great articles.

The public account “Golang is coming” has prepared a mystery learning gift package for you, and the background replies [ebook] to get it!