K
K
kpa6uu2017-07-11 12:00:56
go
kpa6uu, 2017-07-11 12:00:56

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)
  }
}

How can one set datatypes for index , value ?
By this I mean indicating that index is int , value is int .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2017-07-11
@kpa6uu

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 question

Ask a Question

731 491 924 answers to any question