User input validation for integer

PHOTO EMBED

Tue Apr 26 2022 04:19:56 GMT+0000 (Coordinated Universal Time)

Saved by @neuromante70

func isNumeric(s string) int {

	var r int

out:

	for {

		getInt, err := strconv.Atoi(s)

		if err != nil || getInt < 0 {

			fmt.Println("Not a (positive) integer number. Please insert a valid input:")

			fmt.Scan(&s)

		} else {

			if err == nil && getInt > 0 {

				r, _ = strconv.Atoi(s)

				break out

			}

		}

	}

	return r

}
content_copyCOPY

https://github.com/neuromante70/100daysofcode/blob/main/grasshopper/grasshopper.go