In this code, I have created a function TakeInput()
that will take the user input including the whitespaces also. But whenever I run this code and enter the name and school name, it does print the data for me.
Although if I write the scanner
without any function, it takes the data with whitespaces.
package main
import (
"bufio"
"fmt"
"os"
)
func TakeInput(value string) {
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
value = scanner.Text()
}
if err := scanner.Err(); err != nil {
fmt.Println("Error encountered:", err)
}
}
func main() {
var name, school string
fmt.Printf("Enter your name: ")
TakeInput(name)
fmt.Printf("Enter your school name: ")
TakeInput(school)
fmt.Println(name, school)
}