for循环中的golang范围是否保证只被评估一次?
Is a golang range in a for loop guaranteed to be evaluated only once?
在我正在开发的软件中,我看到许多循环如下所示:
for _, i := range strings.Split(s, ",") { ... }
我想确保范围只计算一次,因为拆分字符串总是很昂贵。
golang的文档中有没有这方面的规定?
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.
在我正在开发的软件中,我看到许多循环如下所示:
for _, i := range strings.Split(s, ",") { ... }
我想确保范围只计算一次,因为拆分字符串总是很昂贵。
golang的文档中有没有这方面的规定?
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.