Come testare l'input dell'utente da fmt.Scan/Scanf/Scanln?Come scrivere test contro l'input dell'utente in Go
Ad esempio, come posso verificare che l'ingresso di funzione accetti "4 5 \ n" e "1 2 3 4 \ n" da STDIN e restituisca n == 5 e array == [1, 2, 3, 4].
package main
import (
"fmt"
)
// input gets an array from the user.
func input() (m int, array []int) {
fmt.Print("Enter the size of the array, n, and the difference, m: ")
var n int
_, err := fmt.Scanf("%d %d", &n, &m)
if err != nil {
panic(err)
}
fmt.Print("Enter the array as a space seperated string: ")
array = make([]int, n)
for i := 0; i < n; i++ {
_, _ = fmt.Scan(&array[i])
}
return m, array
}
func main() {
m, array := input()
fmt.Println(m, array)
}
Grazie, ho corretto i bug e inviato una modifica al tuo post. – dmikalova