Todo:golang Pointers Use Precautions

Source: Internet
Author: User

Todo:golang Pointers Use Precautions

650) this.width=650; "class=" Alignnone size-full wp-image-1066 "src=" http://www.todosomeone.com/wp-content/uploads/ 2016/12/127b0005f278c6df2887-1.jpg "width=" "height=" 217 "style=" border:0px;vertical-align:middle;margin:0px Auto;height:auto; "alt=" 127b0005f278c6df2887-1.jpg "/>

Let's take a look at the simple Example 1:

650) this.width=650; "class=" Alignnone size-full wp-image-1067 "src=" http://www.todosomeone.com/wp-content/uploads/ 2016/12/127f0005345650d95482-1.jpg "width=" 511 "height=" 361 "style=" border:0px;vertical-align:middle;margin:0px Auto;height:auto; "alt=" 127f0005345650d95482-1.jpg "/>

Output:

1

1

Example 2:

650) this.width=650; "class=" Alignnone size-full wp-image-1068 "src=" http://www.todosomeone.com/wp-content/uploads/ 2016/12/127b0005f2dfaff1dc93-1.jpg "width=" 489 "height=" 362 "style=" border:0px;vertical-align:middle;margin:0px Auto;height:auto; "alt=" 127b0005f2dfaff1dc93-1.jpg "/>

Output:

1

3

Example 1 is the use of value passing, the Add method does not make any changes; Example 2 is the use of pointers, which change the address, thereby changing the address.

In See Example 3:

650) this.width=650; "class=" Alignnone size-full wp-image-1069 "src=" http://www.todosomeone.com/wp-content/uploads/ 2016/12/128000084871a1b2eb26-1.jpg "width=" 523 "height=" 466 "style=" border:0px;vertical-align:middle;margin:0px Auto;height:auto; "alt=" 128000084871a1b2eb26-1.jpg "/>

Output:

MAP[A:AA B:BB]

Map[b:world A:hello]

Found what, TestMap Front no add *, no pointer, how can also change the value? Because map provides key-value functionality, it is used like a pointer-referenced type. Similar to the type of this feature, arrays are sliced, channel,interface. Go Language Pack This kind of pointer reference type, interested can go to read the source code, the variable is preceded by a "*" number, if not to see if it is a map type.

A copy of a value that does not affect the value of the copy, and the pointer changes all variable names at the same address. Example 4:

650) this.width=650; "class=" Alignnone size-full wp-image-1070 "src=" http://www.todosomeone.com/wp-content/uploads/ 2016/12/127f00053498a1823018-1.jpg "width=" 612 "height=" 465 "style=" border:0px;vertical-align:middle;margin:0px Auto;height:auto; "alt=" 127f00053498a1823018-1.jpg "/>

Output:

tempbytes: [98] 101 102 103 104]

BUF:ABCDEFGH

BUF:DEFGH

TEMP1: [+] D

BUF1:EFGH

Tempbytes is a variable so the value does not change, buf is the pointer, BUF1 is the alias of buf, the BUF or BUF1 is changed by the operation of the value BUF and BUF1. If you copy a BUF value, you have to recreate the same value pointer, example 5:

650) this.width=650; "class=" Alignnone size-full wp-image-1071 "src=" http://www.todosomeone.com/wp-content/uploads/ 2016/12/127b0005f33e6becfa7f-1.jpg "width=" 640 "height=" 446 "style=" border:0px;vertical-align:middle;margin:0px Auto;height:auto; "alt=" 127b0005f33e6becfa7f-1.jpg "/>

Output the results we want:

tempbytes: [98] 101 102 103 104]

BUF:ABCDEFGH

BUF:DEFGH

TEMP1: [] a

BUF1:BCDEFGH

tempbytes: [97 98 99 100 101 102 103 104]

Go comes with garbage collection, so we don't need to manually handle pointer memory management, so we can focus more on the development of code and improve efficiency.

Wxgzh:ludong86

650) this.width=650; "class=" AlignCenter size-full wp-image-845 "src=" http://www.todosomeone.com/wp-content/uploads /2016/11/qrcode_for_gh_6bb1f39ae99c_258-1.jpg "alt=" qrcode_for_gh_6bb1f39ae99c_258-1 "width=" 258 "height=" 258 " style= "border:0px;vertical-align:middle;margin:0px auto;height:auto;text-align:center;"/>


This article is from the computer language driven data blog, so be sure to keep this source http://onetodo.blog.51cto.com/12321875/1880603

Todo:golang Pointers Use Precautions

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.