Understand Go pointers in less than, words or your money back

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

This post was for programmers coming to go who was unfamiliar with the idea of pointers or a pointer type in Go.

What is a pointer?

Simply put, a pointer is a value which points to the address of another. This is the textbook explanation and if you're coming from a language that doesn ' t let's talk about address of a Variab Le, it could very well is written in cuneiform.

Let's break this down.

What is memory?

Computer memory, RAM, can be thought of as a sequence of boxes, placed one after another in a line. Each box, or cell, was labeled with a unique number, which increments sequentially; The address of the cell, its memorylocation.

Each cell is holds a single value. If you know the memory address of a cell, you can go to that cell and read its contents. You can place a value in the that cell; Replacing anything that is in there previously.

That's all there are to know about memory. Everything the CPU does is expressed as fetching and depositing values into memory cells.

What is a variable?

To write a program this retrieves the value stored in memory location $ multiples it by 3 and deposits the result into Memory location 201, we could write something like this in pseudocode:

    • Retrieve the value stored in address $ and place it in the CPU.
    • Multiple the value stored in the CPU by 3.
    • Deposit the value stored in the CPU to memory location 201.


This was exactly how early programs were written; Programmers would keep a list of memory locations, who used it, when, and what the value stored there represented.

Obviously this is tedious and error prone, and meant every possible value stored in memory had to BES assigned an address During the construction of the program. Worse, this arrangement made it difficult to allocate storage to variables dynamically as the program Ran–just imagine if You had to the write large programs using only global variables.

To address this, the notion of a variable was created. A variable is just a convenient, alphanumeric pseudonym for a memory location; A label, or nickname.

Now, rather than talking on memory locations, we can talk about variables, which is convenient names we give to memory Locations. The previous program can now be expressed as:

    • Retrieve the value stored in variable and place it in the a CPU.
    • Multiple it by 3
    • Deposit the value into the variable b .

This was the same program, with one crucial improvement–because we no longer need to talk about memory locations directly, We no longer need to keep track of them–that drudgery are left to the compiler.

Now we can write a program like

var a = 6var B = A * 3

And the compiler would make sure that the variables and is a b assigned unique memory locations to hold their valu E for as long as needed.

What is a pointer?

Now that we know this memory is just a series of numbered cells, and variables be just nicknames for a memory location as Signed by the compiler, what is a pointer?

A pointer is a value, points to the memory address of another variable.

The pointer points to memory address of a variable, just as a variable represents the memory address of Value.

Let ' s has a look at this program fragment

Func Main () {A: = 200b: = &a*b++fmt. Println (A)}

On the first line of main we declare a new variable and a assign it the value 200 .

Next we declare a variable and b assign it the address a . Remember that we don't know the exact memory location where a are stored, but we can still store's a address in c12/>.

The third line was probably the most confusing, because of the strongly typed nature of Go. bcontains the address of variable a , but we want to increment the value stored in a . To does this we must dereference b , follow the pointer from b to a .

Then we add one of the value, and store it back in the memory location stored inb.

The final line prints a the value of, showing that it had increased to 201.

Conclusion

If you're coming from a language with no notion of pointers, or where every variable is implicitly a pointer don ' t panic, Forming a mental model of how variables and pointers relate takes time and practice. Just Remember this rule:

A pointer is a value that points to the memory address of another variable.

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.