This question already has an answer here:
when i write:
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
var ret float64
for z := 1.0, n := 0;n < 10;n++ {
ret = z - (z*z - x) / 2*z
}
return ret
}
func main() {
fmt.Println(Sqrt(2))
}
syntax error: z := 1.0, n used as value. and bring
z := 1.0
out of the for block below
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
var ret float64
z := 1.0
for n := 0;n < 10;n++ {
ret = z - (z*z - x) / 2*z
}
return ret
}
func main() {
fmt.Println(Sqrt(2))
}
it's ok so how can i define two variables in init of the for block?
</div>
A For clause has an Init Statement which is a Simple Statement, including only one Assigment
So in your case, you cannot declare multiple variable with different type/values. You could use a tuple assignment though
for z, n := 1.0, 0; n < 10; n++ {