This is a creation in Article, where the information may have evolved or changed. The latest projects are developed using the go language, so you have the opportunity to get acquainted with the go language. When you write your code, you inadvertently discover a bug in your colleague's code. Come and study with me today. First, the go language has a powerful basic data structure, that is, slices, slices with respect to the array more flexible, dynamic extensibility is excellent. And the go language has a built-in function called Append, the function prototype is as follows: Func append (Slice []type, Elems ... Type) []type, which means that appen can append elements to the slice. Here is a simple example, the example may be a bit hard, I hope you understand. Because of the message interaction, you need to convert the element to a slice of type int, to a slice of the element as a pointer type. So there is the following: (The wrong way for the original code bug Way simplified version) "' Gopackage mainimport" FMT "Func Main () {intslince: = make ([]int, 0) intslince = append ( intslince,9) intslince = append (intslince,8) intslince = append (intslince,7) for _, V: = Range intslince{fmt. Println ("Before convert:", v)}intptrslincea: = Make ([]*int, 0)//Error mode//convert slice element to pointer type for _, V: = Range intslince{ Intptrslincea = Append (Intptrslincea, &v)}//print the slice element for _, V: = Range intptrslincea{fmt. Println ("Wrong:", *v)}intptrslinceb: = Make ([]*int, 0)//correct mode//convert slice element to pointer type for i:= range INTSLINCE{INTPTRSLINCEB = Append (Intptrslinceb, &intslince[i])} Print the slice element for _, V: = Range intptrslinceb{fmt. Println ("Right:", *v)} "runs the result as follows: C:/go\bin\go.exe run E:/GO_CODE/GOPATH/SRC/CONSOLE/APPEND.GO ' ' before Convert:9before convert:8before convert:7wrong:7wrong:7wrong:7right:9right:8right:7 ' messages are sent out, at the receiving end, A message data exception was found. Well before the transformation, the reception is abnormal, explaining the problem of transformation, and then carefully analyze the above code. Traverse the entire slice, assign the element to the TEMP variable V, and append the address of V to the new slice, and the action is complete. The problem is that when the first value is assigned to the temporary variable V and the address of V is appended to the new slice, then the new slice holds the V address, the value is the value of the first element of the original slice, and when the second element variable is assigned, the address of the second element of the new slice is still the address of the temporary variable v. Only the value of this address is the value of the second element of the original slice, so the first element of the new slice is also the address of V, so the value stored in the first element is changed. The result is that, after conversion, the values stored in all elements of the new slice become the values of the last element of the original slice. What can be done to solve the problem? In fact, we can write the code according to the correct practice, directly with the subscript way to find the corresponding address, so there will be no error. 187 reads
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