Golang has pointers. The Go language provides programmers with the ability to control data structure pointers. Its support for pointers is between the Java language and the C/C language. It neither cancels the code's ability to directly operate pointers like Java, nor does it avoid It solves the security and reliability problems caused by the abuse of pointers in C/C. Pointers can be split into two core concepts in the Go language: 1. Type pointer, which allows the data of this pointer type to be modified; 2. Slice, which consists of the original pointer pointing to the starting element, the number of elements, and the capacity.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Different from programming languages such as Java and .NET, the Go language provides programmers with the ability to control data structure pointers, but it cannot perform pointer operations. The Go language allows you to control the data structure of a specific collection, the number of allocations, and the memory access pattern, which is very important for building a well-functioning system. The impact of pointers on performance is self-evident. If you want to do system programming, operating systems or network applications, pointers are an indispensable part.
The Go language's support for pointers is between the Java language and the C/C language. It neither cancels the code's ability to directly operate pointers like Java does, nor does it avoid the need for direct manipulation of pointers in C/C. Security and reliability issues caused by the misuse of pointers.
Pointer (pointer) can be split into two core concepts in the Go language:
type pointer, allowing for this pointer type To modify the data, you can directly use pointers to transfer data without copying the data. Type pointers cannot perform offset and operations.
Slice consists of the original pointer to the starting element, the number of elements, and the capacity.
Benefiting from such constraints and splitting, the pointer type variables of the Go language have the characteristics of efficient pointer access without pointer offset, thus avoiding illegal modification of criticality. Data issues. At the same time, garbage collection makes it easier to retrieve and recycle pointers that will not be offset.
Slices have more powerful features than raw pointers and are safer. When a slice goes out of bounds, the runtime will report a crash and pop up the stack, while the original pointer will only collapse.
What are pointers in golang?
A pointer is a variable whose value is the address of another variable, i.e. the direct address of a memory location. Like a variable or constant, a pointer must be declared before it can be used to store the address of any variable. The general form of a pointer variable declaration is:
var var-name *var-type
Here, var-type is the base type of the pointer; it must be a valid Go data type, and var-name is the name of the pointer variable. The asterisk (*) used to declare a pointer is the same as the asterisk used for multiplication. However, in this statement, the asterisk (*) is used to designate the variable as a pointer. The following is a valid pointer declaration:
var ip *int /* pointer to an integer */ var fp *float32 /* pointer to a float */
The actual data type of the value of all pointers (whether integer, floating point, or other data type) is the same, which represents the long hexadecimal number of the memory address . The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to. [Related recommendations: Go video tutorial]
How to use pointers?
There are several important operations that will be implemented very frequently using pointers.
Define a pointer variable
Assign the address of a variable to a pointer
Finally Access the value of the address available in a pointer variable
This is done by using the unary operator *
to return the value of the variable located at the address specified by the operand. The following example uses these operations:
package main import "fmt" func main() { var a int= 20 /* actual variable declaration */ var ip *int /* pointer variable declaration */ ip = &a /* store address of a in pointer variable*/ fmt.Printf("Address of a variable: %x\n", &a ) /* address stored in pointer variable */ fmt.Printf("Address stored in ip variable: %x\n", ip ) /* access the value using the pointer */ fmt.Printf("Value of *ip variable: %d\n", *ip ) }
When the above code compiles and executes, it produces the following result:
Address of var variable: 10328000 Address stored in ip variable: 10328000 Value of *ip variable: 20
nil pointer in Go
The Go compiler assigns a Nil
value to pointer variables in case the pointer does not have an exact address assigned. This is done when the variable is declared. A pointer specified as a nil
value is called a nil pointer.
nil
Pointers are zero-valued constants defined in several standard libraries. Refer to the following program:
package main import "fmt" func main() { var ptr *int fmt.Printf("The value of ptr is : %x\n", ptr ) }
When the above code compiles and executes, it produces the following result:
The value of ptr is 0
On most operating systems, the program is not allowed to access the address 0# The memory at ## because this memory is reserved by the operating system. However, memory address
0 has special meaning; it indicates that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains a
nil (zero) value, it is assumed to point to nothing.
nil pointer, you can use the
if statement, as shown below:
if(ptr != nil) /* succeeds if p is not nil */ if(ptr == nil) /* succeeds if p is null */
Programming Video! !
The above is the detailed content of Does golang have pointers?. For more information, please follow other related articles on the PHP Chinese website!