Go Cheatsheet - Get User Input from Stdin
Get User Input from Stdin
Scanln & Scanf
var name string
fmt.Scanln(&name) // this will scan only one string token (if there is a space in your string)
var age int
fmt.Scanf("%d\n", &age)
bufio.NewScanner
sc := bufio.NewScanner(os.Stdin)
sc.Scan()
txt := sc.Text() // read one line and save text to txt, it will contain spaces and exclude the ending \n
fmt.Println(txt)
bufio.NewReader
nr := bufio.NewReader(os.Stdin)
input, _ := nr.ReadString('\n') // read till \n, the returned string will contain the trailing \n
input = strings.TrimRight(input, "\n") // we remove the trailing \n
fmt.Println(input)