The hands of the Go language

Source: Internet
Author: User

Array Arrays

An array is a built-in (build-in) type, a collection of data of the same type, which is a value type that accesses element values through a subscript index starting at 0. The length is fixed after initialization, and its length cannot be modified. When a parameter is passed in as a method, an array is copied instead of referencing the same pointer. The length of the array is also part of its type, and its length is obtained through the built-in function len (array).

Learn pointers and addresses before you begin:

The go language pointer is very easy to learn, the go language using pointers can be more simple to perform some tasks, the variable is a convenient placeholder for reference to the computer memory address, the Go language address is &, and before a variable is used to return the memory address of the corresponding variable.

Package Main
Import "FMT"
Func Main () {
var a int = 10
Fmt. Println (&a)
}

Results of execution:

C:/users/cherry/go/src/test/test.exe [C:/users/cherry/go/src/test]

0xc0420381d0

Pointer:

A pointer variable can point to the memory address of any one of the values it points to the memory address of the value, similar to variables and constants, you need to declare the pointer before using the pointer. The pointer declaration format is as follows:


var var_name *var-typevar-type is a pointer type, var_name is the pointer variable name, and the * number is used to specify that the variable is as a pointer

Use process:

1. Define pointer variables

2. Assigning values to pointer variables

3. Access the value in the pointer variable that points to the 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//Get the storage address of the variable
Fmt. PRINTF ("Storage address for IP variable:%x\n", IP)
Fmt. Printf ("The address of a variable is:%x\n", &a)
Fmt. Printf ("Value of *IP variable:%d\n", *ip)
}

Execution Result:

C:/users/cherry/go/src/test/test.exe [C:/users/cherry/go/src/test]

Storage address of IP variable: c0420381d0

The address of the A variable is: c0420381d0

The value of the *IP variable: 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 to PTR

Go pointer Array

Egg

Package Main
Import "FMT"
const MAX int = 3

Func Main () {
A: = []int{10, 20, 30}
var i int
For I: = 0; i < Max; i++ {
Fmt. Printf ("a[%d]=%d\n", I, A[i])
}
}

Results:

C:/users/cherry/go/src/test/test.exe [C:/users/cherry/go/src/test]

a[0]=10

A[1]=20

A[2]=30

In one case, we may need to save the array so that we need to use the pointer, and the following declares an array of int pointers:

var ptr [Max] *int//ptr is an integer pointer array. So each element points to a value

Egg

Package Main
Import "FMT"
const MAX INT = 3
Func Main () {
A: = [...] INT{10, 20, 30}
var i int
var ptr [max]*int
For I: = 0; i < MAX; i++ {
Ptr[i] = &a[i]//integer address assignment to pointer array
}
For I: = 0; i < MAX; i++ {
Fmt. Printf ("a[%d]=%d\n", I, *ptr[i])
}
}

If a pointer variable holds the address of another pointer variable, it is called a pointer variable pointing to the pointer, when a pointer variable is defined, 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

Accessing pointer variable values that point to pointers requires the use of two * number egg:

Package Main
Import "FMT"
Func Main () {
var a int
var ptr *int
var pptr **int
A = 300
PTR = &a
Pptr = &ptr
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)
}

Results of execution:

C:/users/cherry/go/src/test/test.exe [C:/users/cherry/go/src/test]

Variable a=300

Pointer variable *ptr=300

Pointer to pointer variable **pptr=300


This article is from the "Dbaspace" blog, make sure to keep this source http://dbaspace.blog.51cto.com/6873717/1962925

The hands of the Go language

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.