This really depends on the Go version but go version go1.7 windows/amd64
does appear to calculate the value once.
Go code:
var cycles = 10.0
var res = 1000.0
for t := 0.0; t < cycles*2*math.Pi; t += res {
}
Asm code:
movsd [rsp+58h+var_20], xmm0
mov [rsp+58h+var_18], 0
mov [rsp+58h+var_10], 0
lea rax, qword_494500
mov [rsp+58h+var_58], rax
lea rax, [rsp+58h+var_20]
mov [rsp+58h+var_50], rax
mov [rsp+58h+var_48], 0
call runtime_convT2E
mov rax, [rsp+58h+var_40]
mov rcx, [rsp+58h+a] ; a
mov [rsp+58h+var_18], rax
mov [rsp+58h+var_10], rcx
lea rax, [rsp+58h+var_18]
mov [rsp+58h+var_58], rax
mov [rsp+58h+var_50], 1
mov [rsp+58h+var_48], 1
call fmt_Println
movsd xmm0, cs:$f64_408f400000000000
movsd xmm1, [rsp+58h+t]
addsd xmm0, xmm1
movsd [rsp+58h+t], xmm0
movsd xmm1, cs:$f64_404f6a7a2955385e
ucomisd xmm1, xmm0
ja loc_401083
f64_404f6a7a2955385e
is a precalculated double value equal to 10 * 2 * math.Pi
or 62.8318530718
Go compiler recently switched to SSA, so these kind of optimizations will just keep improving as they greatly benefit from it. For now SSA is only available on amd64:
Compiler Toolchain
This release includes a new code generation back end for 64-bit x86
systems, following a proposal from 2015 that has been under development
since then. The new back end, based on SSA, generates more compact, more
efficient code and provides a better platform for optimizations such as
bounds check elimination.
1.8 should have it for all supported architectures:
Compiler Toolchain
Go 1.7 introduced a new compiler back end for 64-bit x86 systems. In Go
1.8, that back end has been developed further and is now used for all
architectures.