thanks! v must be a value copy,Is this determined by the compiler? 在 2019年4月3日星期三 UTC+8下午7:57:26,Marvin Renich写道: > > * mount...@gmail.com <javascript:> <mount...@gmail.com <javascript:>> > [190403 05:10]: > > package main > > > > func main() { > > b := []int{1} > > > > bb := make([]*int, 0, 1) > > for k, v := range b { > > The above range clause will assign to k the index of the current > element, and it will assign to v a copy (as if v = b[k]) of the element > at that index. > > Go does not have references of the kind that C++ or Java has, so v > cannot be a reference, in that sense, to b[k]; it must be a value copy. > > If what you really want is to be able to refer to the location of the > element in the slice (really its backing array), omit the v in the range > clause and just use &b[k]: > > for k := range b { > _ = &b[k] // do what you want > } > > > _ = &v //8 line > > _ = &b[k] //9 line > > // bb = append(bb, &v) > > bb = append(bb, &b[k]) > > } > > > > // for _, v := range bb { > > // fmt.Println(*v) > > // } > > } > > ...Marvin > >
-- 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 https://groups.google.com/d/optout.