Value copy of common errors in golang and single variable explanation in for loop

Time:2020-1-9

Preface

Golang (Chinese Name: go language) is the second open source programming language released by Google in 2009. Go language is specially optimized for the application programming of multiprocessor system. The program compiled by go can be as fast as C or C + + code, and it is more secure and supports parallel process.. If you want to know more, please go to the official website of golang

The function call in go copies copy value, and the variable of V is always a variable in the for loop. If V is a pointer, the print method receives a copy of the pointer. The pointer value of each iteration of V in the for loop body is different, so the output is different.

In the article of common errors in go, http://devs.cloudimmunity.com/gotchas-and-common-hosts-in-go-golang/


package main

import ( 
 "fmt"
 "time"
)

type field struct { 
 name string
}

func (p *field) print() { 
 fmt.Println(p.name)
}

func main() { 
 data := []field{{"one"},{"two"},{"three"}}

 for _,v := range data {
 go v.print()
 }

 time.Sleep(3 * time.Second)
 //goroutines print: three, three, three
}

Changing the type of field slice to pointer results in different results:


package main

import ( 
 "fmt"
 "time"
)

type field struct { 
 name string
}

func (p *field) print() { 
 fmt.Println(p.name)
}

func main() { 
 data := []*field{{"one"},{"two"},{"three"}}

 for _,v := range data {
 v := v
 go v.print()
 }

 time.Sleep(3 * time.Second)
 //goroutines print: one, two, three
}

How does the difference between the two pieces of code lead to different results?

I have modified the part of the code for loop above. After the transformation, the corresponding codes are as follows:

Slice yes no pointer

data := []field{{"one"},{"two"},{"three"}}

 for _,v := range data {
 pp := (*field).print
 Go PP // non pointer
 }

Slice is a pointer


 data := []*field{{"one"},{"two"},{"three"}}

 for _,v := range data {
 pp := (*field).print
 go pp(v) // pointer
 }

After the transformation, you can see the most obvious difference in the transmission of the receiver of the print method by looking at the original code.

The function call in go copies copy value, and the variable of V is always a variable in the for loop.

If V is a pointer, the print method receives a copy of the pointer. The pointer value of each iteration of V in the for loop body is different, so the output is different.

If V is a normal struct, every iteration in the for loop body & V is the pointer of the variable itself, that is, it always points to the same field. Because to a large extent, goroutine in this code is executed after the end of for, and V will point to the last field, that is {“three”}, so the output is the same.

It is said that the random output of one, two and three is caused by the multi-core CPU. If the CPU is changed to a single core, it is a sequential output, which is not particularly accurate. Theoretically, goroutine scheduling is random, that is to say, even single core output may be random, but when such a simple example is run, the general machine environment will not cause cross execution of these three simple goroutines. For example, you can simulate IO busy before print output to achieve the goal that even single core may be random output.


 if rand.Intn(100) > 20 {
 time.Sleep(1 * time.Second)
 }

summary

The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.

Recommended Today

[reading notes] calculation advertising (Part 3)

By logm This article was originally published at https://segmentfault.com/u/logm/articles and is not allowed to be reproduced~ If the mathematical formula in the article cannot be displayed correctly, please refer to: Tips for displaying the mathematical formula correctly This article isComputing advertising (Second Edition)Reading notes. This part introduces the key technology of online advertising, which is […]