Let’s see how to scan string and int in golang. You need to import the following packages for operating on strings.
Scan a string
package main import ( "bufio" "errors" "fmt" "os" "strings" ) func main() { fmt.Println("Please enter a String") in := bufio.NewReader(os.Stdin) line, err := in.ReadString('\n') readLine := strings.TrimSuffix(line, "\n") readLine = strings.ToLower(readLine) if err != nil { errors.New("NullPointerException") } else { fmt.Println(readLine) } } <p> </p>
Output
Please enter a string hello hello
Scanning int
package main import ( "errors" "fmt" ) func main() { fmt.Println("Please enter a int value") var i int _, err := fmt.Scanf("%d", &i) if err != nil { errors.New("NullPointerException") } else { fmt.Println(i) } } <p> </p>
Output
Please enter a int value 123 123
Also check: How to install golang on ubuntu