In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "what are the common problems with Go language pointers?". In the operation of practical cases, many people will encounter such a dilemma. Next, 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!
In the GE language, pointers actually have the following forms: first: pointer; second: interface; third: slice; fourth: map.
Scenario 1: when a pointer variable is used as a parameter, a copy of the pointer variable will be copied
The most common and confusing thing about pointers is, when passed as a parameter, does the pointer copy a pointer variable, or is it similar to the C++ reference, using the pointer variable before it is passed in?
Example:
Output:
Introduction:
From the output of the above example, no matter it is pointer, interface, slice or map, as long as they are passed into the function as arguments, they all copy a pointer variable, and the address of the pointer variable in the main function is not the same as that in the test function.
Pointer variables in main and test point to the same address, and even changes to pointers, slice and map data in test will take effect in main.
Of course, we can also come to the conclusion that interfaces, slice and map are passed as parameters in the same way as using pointer variables.
Scenario 2: pointer variables are best not used in go statements
The use of co-programs in Go language should be particularly careful, because the underlying implementation mechanism of goroutine cannot guarantee which co-programs will be executed first, that is, the execution order of co-programs is unpredictable. In this way, if you change the value of the same block of memory within the co-program, the output result is not guaranteed, so we had better not modify the value of the same block of memory inside the co-program.
Example:
The first and second Output:
Third Output:
Introduction:
Through the above example, we can find that the mechanism of goroutine causes the operation order of the tmp of the pointer to be inconsistent, resulting in the output to change the data value of the pointer is different, so when we use go, it is best not to use pointer variables as parameters. The specific scenarios are as follows:
Scenario 1: if you just read the data in the pointer, you can use the pointer variable as the parameter, which is not recommended, unless the structure of the parameter is very large, and it is very time-consuming to copy a data structure.
Scenario 2: if the data change in the pointer has nothing to do with the execution order of the goroutinue, you can use the pointer variable as a parameter.
Scenario 3: the change of the data in the pointer is related to the order of execution, and the pointer variable cannot be used as a parameter.
Scenario 3: when recycling slice, change the impact of the data in it
The following example doesn't have much to do with pointers, but people often get confused, especially if C++ developers transfer to Go, because it's easy to compare with iterators in C++.
Example 1:
Introduction: as can be seen from the output, changing the value of n will not change the data in the nodes array. The reason is that the replication operation of n in the use of range.
Example 2:
Description: if you want to make changes to the data in the array nodes, you can use the subscript method. The reason is that the data elements in the nodes corresponding to the subscript are the values in the array, which is equivalent to changing the value of the original variable.
Example 3:
Introduction: you can also change the elements in the array into pointers, so that n in range is equivalent to operating on pointers, and the data corresponding to pointers will be changed at the same time.
Example 4:
Introduction: after deleting the array nodes pointed to by range, we found that the corresponding value "the" in nodes was deleted. However, the index in range has not been changed and still becomes 2, which leads to confusion in subsequent nodes operations.
Scenario 4: when recycling map, change the impact of the data in it
For examples 1 and 2 in scenario 3, they are consistent in the map scenario, and the reason is referred to scenario 3.
Example:
Introduction:
From the output, we can see that in the range operation of map, even if we delete an element, the value in map will be updated immediately, and the subsequent k value will also use the latest value. The reason is that map uses the correspondence of (Key,value). After the map is updated, the value of key will be updated.
This is the end of the content of "what are the common problems with 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
Spackage com.iteye.lindows.mysql/** * @ author Lindows * * / class TestGroovy {static main (args) {
© 2024 shulou.com SLNews company. All rights reserved.