Re: [go-nuts] differences between pointer and value slice in for-range loop

2016-09-19 Thread Marvin Renich
* Fei Ding [160918 02:58]: > Thanks, Marvin, I've learned a lot from your reply. And, I've written more > code, like: > > a, b, c := 1, 2, 3 > > slice1 := []int{a, b, c} > > for _, n := range slice1 { > > go func(n *int) {fmt.Println(*n)}(&n) > > } > > > It seems that pass *n's address *in the

Re: [go-nuts] differences between pointer and value slice in for-range loop

2016-09-18 Thread Marvin Stenger
https://play.golang.org/p/bVizbGIMDp should work -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit

Re: [go-nuts] differences between pointer and value slice in for-range loop

2016-09-18 Thread Marvin Stenger
https://play.golang.org/p/9LQMDrDIOv should work. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visi

Re: [go-nuts] differences between pointer and value slice in for-range loop

2016-09-17 Thread Fei Ding
Thanks, Marvin, I've learned a lot from your reply. And, I've written more code, like: a, b, c := 1, 2, 3 > slice1 := []int{a, b, c} > for _, n := range slice1 { > go func(n *int) {fmt.Println(*n)}(&n) > } It seems that pass *n's address *in the code above will make a data race, which you have

Re: [go-nuts] differences between pointer and value slice in for-range loop

2016-09-17 Thread Marvin Renich
* Fei Ding [160916 23:30]: > Link here: https://play.golang.org/p/cdryPmyWt5 > > The code above is going to check the differences between pointers and > values in a for loop, while go statement is also used at the same time. For > code: > > values := []field{{"one"},{"two"},{"three"}} >

[go-nuts] differences between pointer and value slice in for-range loop

2016-09-16 Thread Fei Ding
Hi: Please check this code snippet: package main import ( "fmt" "time" ) type field struct { name string } func (p *field) print() { fmt.Println(p.name) } func main() { fmt.Println("use values:") // use values in range loop and go rountines values := []field