Home > Backend Development > Golang > The difference between golang references and pointers

The difference between golang references and pointers

(*-*)浩
Release: 2019-12-27 13:35:11
Original
9514 people have browsed it

The difference between golang references and pointers

Some languages, including C and C, support pointers. Other languages ​​including C, Java, Python, Ruby, Perl, and PHP all support quoting. On the surface, references and pointers are very similar. They are both used to allow one variable to provide access to another variable.                                                                                                                                                                                                                                        (Recommended learning: go)

Since both provide many of the same functions, it is often unclear how their respective internal mechanisms differ. In this article, I will explain the difference between pointers and references.

Why this matters

Pointers are the core of the Go language. Most programmers learn Go based on one of the languages ​​mentioned above. Therefore, understanding the difference between pointers and references is crucial to understanding Go.

Even if you have worked with a language that has pointers, Go's implementation of pointers is different from C and C because it retains some of the nice properties of references while retaining the functionality of pointers.

The remainder of this article is intended to discuss the concept of references broadly, rather than specific implementations. We will use Go as the reference implementation of pointers.

what is the difference?

A pointer variable stores the address of another variable.

A reference variable points to another variable.

To prove our point, use C as an example, which supports both pointers and references.

int i = 3;2int *ptr = &i;3int &ref = i;
Copy after login

The first line defines variable i; the second line defines a pointer ptr pointing to the memory address of variable i; the third line defines a reference ref pointing to variable i.

Not only are the operators different, but the methods used are also different. For pointers, you must use the * operator to dereference. For references, no operators are required. It is understood that you tend to use quoted variables.

Continuing with our example, the following two lines of code will both change the value of i to 13.

ptr = 13;
ref = 13;
Copy after login

You may ask, if I try to access the variable ptr directly without using dereference. This brings us to the second key difference between pointers and references. Pointers can be reallocated, references cannot. In other words, the pointer can be assigned a different address.

The above is the detailed content of The difference between golang references and pointers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template