In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "detailed introduction of Go language pointers". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The Go language provides programmers with the ability to control pointers of data structures, but cannot perform pointer operations. The Go language allows you to control the data structure of a particular collection, the number of allocations, and memory access patterns, which is important for building a well-functioning system. The impact of pointers on performance is self-evident, and pointers are an indispensable part if you want to do system programming, operating systems, or network applications.
Pointer can be divided into two core concepts in GE language: type pointer, which allows data of this pointer type to be modified, pointer can be directly used to transfer data without copying data, and type pointer can not be offset and calculated. A slice consisting of the original pointer to the starting element, the number of elements, and the capacity.
Benefiting from such constraints and splitting, pointer type variables in Go language not only have the characteristics of efficient pointer access, but also do not have pointer offset, thus avoiding the problem of illegally modifying critical data. At the same time, garbage collection is also easier to retrieve and collect pointers that will not shift.
Slices have more powerful features and are more secure than the original pointer. When the slice is out of bounds, the runtime reports an outage and hits the stack, while the original pointer only crashes.
Pointers in Cmax Cobb +
When it comes to pointers in Chammer +, it will make a lot of people talk about it, especially the offset, operation and conversion of pointers.
In fact, pointers are the foundation of the extremely high performance of CCompact + language, which is convenient and convenient to manipulate large chunks of data and make offsets. Therefore, the operating system is still written in C language and the characteristics of pointers.
The fundamental reason why pointers are widely criticized is the operation and memory release of pointers. The bare pointers in the language can be offset freely, and even enter the core area of the operating system in some cases. Our computer operating system often needs to update and fix the essence of vulnerabilities, which is to solve the problem of "buffer overflow" caused by pointer out-of-bounds access.
To understand pointers, you need to know several concepts: pointer addresses, pointer types, and pointer values, which are described in more detail below.
Recognize pointer address and pointer type
A pointer variable can point to the memory address of any value, and the memory address of the value it points to occupies 4 or 8 bytes on 32-bit and 64-bit machines, respectively, regardless of the size of the value it points to. When a pointer is defined and not assigned to any variable, its default value is nil. Pointer variables are usually abbreviated to ptr.
Each variable has an address at run time that represents the location of the variable in memory. In the Go language, the & operator (prefix) is added to the variable name to obtain the memory address of the variable (address operation) in the following format:
The type of ptr: & v / / v is T
Where v represents the variable to be addressed, the address of the variable v is received using the variable ptr, and the type of ptr is * T, which is called the pointer type of T, and * represents the pointer.
The actual use of pointers can be seen through the following example:
Package mainimport ("fmt") func main () {var cat int = 1var str string = "banana" fmt.Printf ("% p% p", & cat, & str)}
Running result:
0xc042052088 0xc0420461b0
The code is as follows: line 8, declare the integer variable cat. Line 9 declares the string variable str. In line 10, use the verb% p of fmt.Printf to print the memory addresses of the cat and str variables, and the value of the pointer is a set of data with the 0x hexadecimal prefix.
Tip: the relationship among variables, pointers and addresses is that each variable has an address, and the value of the pointer is the address.
Get the value that the pointer points to from the pointer
When you use the & operator to address an ordinary variable and get the pointer of the variable, you can use the * operator, that is, the pointer, to take the value of the pointer, as follows.
Package mainimport ("fmt") func main () {/ / prepare a string type var house = "Malibu Point 10880, 90265" / / A pair of string addresses, ptr type * stringptr: = & house// print ptr type fmt.Printf ("ptr type:% T\ n", ptr) / / print ptr pointer address fmt.Printf ("address:% p\ n") Ptr) / / A pair of pointers perform value operation value: = * the type fmt.Printf ("value type:% T\ n", value) / / the pointer points to the variable fmt.Printf ("value:% s\ n", value)}
Running result:
Ptr type: * string
Address: 0xc0420401b0
Value type: string
Value: Malibu Point 10880, 90265
The code is as follows: line 10, prepare a string and assign it. On line 13, address the string and save the pointer to the variable ptr. On line 16, print the type of the variable ptr, which is * string. On line 19, print the pointer address of the ptr, which changes each time you run. In line 22, you take a value on the ptr pointer variable, and the variable value is of type string. Line 25, the type of value after printing the value. Line 28, print the value of value.
The address operator & and the value operator * are a pair of complementary operators & take out the address and * take out the value pointed to by the address.
The relationships and characteristics of variables, pointer addresses, pointer variables, addresses and values are as follows: the pointer variable of the variable can be obtained by using the & operator to take the address of the variable. The value of the pointer variable is the pointer address. Using the * operator to take the value of the pointer variable, you can get the value of the original variable that the pointer variable points to.
Use pointers to modify values
The pointer can not only take the value, but also modify the value.
Previously, we have demonstrated the use of multiple assignments for numerical exchange, which can also be done using pointers, as shown in the following code:
Package mainimport "fmt" / / the exchange function func swap (a, b * int) {/ / takes the value of a pointer, assigns it to the temporary variable tt: = * a pointer / takes the value of b pointer, assigns the variable pointed to by a pointer * a = * b] / assigns the value of a pointer to the variable pointed by b pointer * b = t} func main () {/ prepares two variables, assigning values 1 and 2x, y: = 1 2Universe / swap variable value swap (& x, & y) / / output variable value fmt.Println (x, y)}
Running result:
2 1
The code is as follows: line 6, define an exchange function with arguments an and b, and the types are all * int pointer types. In line 9, take the value of pointer an and assign it to the variable tmenet, which is now of type int. In line 12, take the pointer value of b and assign it to the variable pointed to by pointer a. Note that at this time * a does not mean to take the value of a pointer, but "the variable that a points to". In line 15, assign the value of t to the variable pointed to by pointer b. In line 21, prepare two variables, x and y, with values of 1 and 2, and type int. On line 24, take the addresses of x and y and pass them as arguments to the swap () function to make the call. In line 27, when the exchange is complete, output the values of x and y.
When the * operator is used as the right value, the meaning is to take the value of the pointer as the left value, that is, when it is placed to the left of the assignment operator, to indicate the variable that the a pointer points to. In fact, to sum up, the fundamental meaning of the * operator is to manipulate the variables pointed to by the pointer. When the operation is at the right value, it takes the value that points to the variable, and when the operation is at the left value, it sets the value to the variable that points to.
What happens if the pointer value is exchanged in the swap () function? Please refer to the following code:
Package mainimport "fmt" func swap (a, b * int) {b, a = a, b} func main () {x, y: = 1, 2swap (& x, & y) fmt.Println (x, y)}
Running result:
1 2
The results show that the exchange is not successful. The swap () function in the above code exchanges the addresses of an and b, and after the exchange, the variable values of an and b are indeed swapped. But the two variables associated with an and b are not actually associated. It's like a card with two houses spread out on the table, and exchanging cards from two houses won't have any effect on the two houses.
Example: use pointer variables to get input information from the command line
The flag package built into the Go language implements the parsing of command line parameters, and the flag package makes it easier to develop command line tools.
The following code can get the command line data after parsing the flag package by defining some command line instructions and corresponding variables in advance, and entering the corresponding parameters at run time.
[example] get command line input:
Package main// Import system package import ("flag"fmt") / / define command line parameter var mode = flag.String ("mode", "", "process mode") func main () {/ / parse command line parameter flag.Parse () / / output command line parameter fmt.Println (* mode)}
Name this code main.go and run it using the following command line:
Go run main.go-mode=fast
The output from the command line is as follows:
Fast
The code is as follows: line 10, through flag.String, defines a mode variable of type * string. The next three parameters are as follows: parameter name: use this name when entering parameters on the command line. Default values of parameter values: corresponding to the function creation variable type used by flag, String corresponds to string, Int corresponds to integer, Bool corresponds to Boolean, and so on. Parameter description: when using-help, it will appear in the description. On line 15, parse the command line arguments and write the results to the variable mode. On line 18, print the variable pointed to by the mode pointer.
Since a command line parameter named mode has been registered with flag.String, the bottom layer of flag knows how to parse the command line and assign the value to the mode*string pointer. After the Parse call is completed, there is no need to get the value from flag, but the final value is obtained through the mode pointer registered by yourself. The code runs as shown in the following figure.
Figure: relationship between command line arguments and variables
Another way to create pointers-- the new () function
The Go language also provides another way to create pointer variables in the following format:
New (type)
It's usually written like this.
Str: = new (string) * str = "Go language tutorial" fmt.Println (* str)
The new () function creates a pointer of the corresponding type, the creation process allocates memory, and the created pointer points to the default value.
This is the end of the detailed introduction of Go language pointers. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Select RIGHT ('00 + Datename (DD,GETDATE ()), 2)
© 2024 shulou.com SLNews company. All rights reserved.