Answer the question
In order to leave comments, you need to log in
How to specify variable types in foreach Golang?
Greetings!
In Golang , foreach is implemented by the following code:
package main
import (
"fmt"
)
func main() {
var data [2]int = [2]int{
1,
2,
}
for index, value := range data {
fmt.Println(index, "=", value)
}
}
Answer the question
In order to leave comments, you need to log in
Your types are automatically inferred here because you are using :=
. They are int, int
.
You can declare these variables before the loop and use =
, but still you won’t be able to use other types, since go has strong static typing, and range in this case returns youint, int
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question