Comments inline below: On Friday, December 24, 2021 at 10:08:23 AM UTC-7 cuiw...@gmail.com wrote:
> in the lang spec <https://go.dev/ref/spec#For_statements>, it says: > *The range expression x is evaluated once before beginning the loop, with > one exception: if at most one iteration variable is present and len(x) is > constant, the range expression is not evaluated.* > > https://go.dev/play/p/frLAvStOrDu > > > first range expr will be evaluated. > > second range expr will not be evaluated. > > third range expr will be evaluated. > > is this correct? > Those three statements seem correct to me. The easiest way to check is to pass a nil array, which will panic if evaluated and not panic if not evaluated. For example: https://go.dev/play/p/P1DDtZKy2jj > > and > > does the range expr will be evaluated means when range expr is slice, the > slice header will be copy, and when range expr is array, array will be coy? > I believe this is true, but I don't quite know why it is guaranteed in the specification. Someone else may be able answer this question with more evidence. -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/d078d6b8-f928-47e9-b10b-a0ae224fd123n%40googlegroups.com.