Golang's hands

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

How to use pointers

Pointer usage Flow:

    • Defines a pointer variable.
    • Assigns a value to a pointer variable.
    • Accesses a value in a pointer variable that points to an address.

Precede the pointer type with an * sign (prefix) to get the content pointed to by the pointer

Package Main

Import "FMT"

Func Main () {
var a int = 20
var IP *int
IP = &a
Fmt. Println (&a)
Fmt. PRINTLN (IP)
Fmt. Println (*IP)

}

Code Run Result:

C:\users\xiaoj\desktop>go Build Pointer.go

C:\users\xiaoj\desktop>pointer.exe
0xc0420080c0
0xc0420080c0
20

Go NULL pointer

When a pointer is defined and not assigned to any variable, its value is nil.

The nil pointer is also known as a null pointer.

Nil, which is conceptually null, none, nil, and Null in other languages, refers to a value of 0 or null.

A pointer variable is usually abbreviated as PTR.

View the following instances:

Package Main

Import "FMT"

Func Main () {
var ptr *int
Fmt. Println ("%x\n", PTR)
}

Example output:

C:\users\xiaoj\desktop>blank_pointer.exe
%x
<nil>

Go pointer Array

Package Main

Import "FMT"

const MAX INT = 3

Func Main () {

A: = []int{10,100,200}
var i int

For i = 0; i < MAX; i++ {
Fmt. Printf ("a[%d] =%d\n", I, A[i])
}
}

The above code executes the output as:

A[0] = 10
A[1] = 100
A[2] = 200

In one case, we may need to save the array so that we need to use the pointer.

An integer pointer array is declared as follows:

var ptr [Max]*int;

PTR is an array of integer pointers. Therefore, each element points to a value. The three integers for the following instances are stored in the array of pointers:

Package Mainimport "FMT" const MAX int = 3func Main () {   A: = []int{10,100,200}   var i int   var ptr [max]*int;
  
   for  i = 0; i < Max; i++ {      ptr[i] = &a[i]/* Integer address assigned to array of pointers */   } for  i = 0; i < Max; i++ {      Fmt. Printf ("a[%d] =%d\n", I,*ptr[i])   }}
  

The above code executes the output as:

A[0] = 10a[1] = 100a[2] = 200

If a pointer variable holds the address of another pointer variable, the pointer variable is called a pointer variable pointing to the pointer.

When you define a pointer variable pointing to a pointer, the first pointer holds the address of the second pointer, and the second pointer holds the address of the variable:

Pointer to pointer variable declaration format as follows:

var ptr **int;

The pointer variable above points to the pointer is an integral type.

Accessing pointer variable values that point to pointers requires the use of two * numbers, as follows:

Package Mainimport "FMT" Func Main () {   var a int   var ptr *int   var pptr **int   A =   */* pointer ptr address */
  ptr = &a/   * points to the pointer ptr address */   pptr = &ptr/   * Gets the value of Pptr */   FMT. Printf ("Variable a =%d\n", a)   FMT. Printf ("pointer variable *ptr =%d\n", *ptr)   FMT. Printf ("Pointer to pointer variable **pptr =%d\n", **pptr)}

The output of the above instance execution is:

Variable A = 3000 pointer variable *ptr = 3000 pointer to pointer variable **PPTR = 3000


Go image function Pass pointer parameter

The Go language allows you to pass pointers to functions, which you need to set as pointer types on the parameters defined by the function.

The following example demonstrates how to pass a pointer to a function and modify the value within the function after the function call:

Package Mainimport "FMT" Func Main () {/   * defines local variables */   var a int =   var b int=   . Printf ("The value of pre-exchange a:%d\n", a)   FMT. Printf ("Value of pre-swap B:%d\n", b)/   * Call function to exchange value   * &a point to the address of a variable   * &b point to the address of the B variable *   /swap (&a, &am P;B);   Fmt. Printf ("Value after exchange a:%d\n", a)   FMT. Printf ("Value after swap B:%d\n", b)}func swap (x *int, y *int) {   var temp int   temp = *x/    * Save value for x Address */   *x = *y/      * assigns y to X */   *y = temp/    * Assigns temp to Y */}

The above example allows the output to be:

Value before exchange A: 100 value of pre-swap B: 200 value After exchange a: 200 value after Exchange B: 100

408 Reads
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.