I've only just started using go so sorry in advance if any of my questions are very obvious but I've spent a long time looking for answers online and couldn't find what I was looking for.
I want to read a line of space separated int values and store them into an array. The number of values will be known before they are input but I'm fairly sure there must be a more simple or neat way of writing it than this:
package main
import "fmt"
func main() {
var array[5] int
fmt.Scanf("%d %d %d %d %d", &array[0], &array[1], &array[2], &array[3], &array[4])
fmt.Printf("%v", array)
}
I also need to make it so that the number of values can be changed (to up to 50) and they should still be stored in the array. I wrote a simple function to make the scanf function read the right amount of numbers but without writing out each array element all the way out to &array[49] I couldn't find a work around. Is there an easier way of storing all the values without having a really long list of array locations?
package main
import "fmt"
func main() {
var length int
fmt.Scanf("%d", &length)
var array[50] int
fmt.Scanf(scanFormat(length), &array[0], &array[1], &array[2]) //etc.
fmt.Printf("%v", array)
}
func scanFormat (N int) string{
if N==0 {
return ""
}
return "%d "+scanFormat(N-1)
}