How to read user input from console and print in Golang examples
- Admin
- Dec 31, 2023
- Golang-examples
In this blog post, input data can be read from the console in many ways in the Go language.
- read input values using the scan function
- read multiple input values from the console
- read user input line by line using stdin stream
How to read user input using the golang scan function example
In this section, you learned how to read user input using the scan function.
here is an example.
package main
import (
"fmt"
)
func main() {
//reading a string
var name string
fmt.Print("What is your Name? ")
fmt.Scan( & name)
fmt.Println("Entered Name is :", name)
}
Output:
What is your Name? kiran babu
Entered Name is: kiran
In the above example, a String variable is declared.
package fmt
has a Scan
function, fmt package
is imported into the program.
Function Scan
is used to read the user input from the standard input i.e console. input values are stored with the separation of space into successive parameters.
You can check more about scanđź”— function. Scan function
store the address or pointer of the variable.
What is your Name? prompt is displayed to the end-user to take input from the user
fmt.Scan(&name)
function read all values separated in spaces until newline(\n or enter keyword) entered. Each word separated by spaces is stored in a variable name.
user types the string “kiran babu”
As we declared only one variable - name, text “kiran babu” will be treated as two variables in space-separated format, the first value is stored in the variable name.
Finally, printing entered a value into the console.
The only disadvantage with this is that entered text is saved in multiple variables, not a complete line.
Here is an example for reading multiple input values
package main
import (
"fmt"
)
func main() {
//reading an multiple input string
var fname string
var lname string
fmt.Print("What is your Name? ")
fmt.Scanln( & fname, & lname)
fmt.Printf("Entered Name is %s %s", fname, lname)
}
Output:
What is your Name? Cloud Hadoop
Entered Name is Cloud Hadoop
The next example is to read user input line by line using the stdin stream.
How to read user input line using golang stdin stream function
Bufio package
provides to read/write from any stream.
It has a Scanner
class to read line by line from the input console
bufio.NewScanner which accepts io.Reader to scan line by line from a given stream.
bufio.NewScanner(os.Stdin) read data from keyword line by line from command line console.
Finally, scanner.text() stores the user text and print it to console
Following is read input line by line from command line console using stdin stream
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main() {
fmt.Println("Pleae enter text")
scanner: = bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fmt.Println("Entered Text is ", scanner.Text())
}
// Error handling
if err: = scanner.Err();
err != nil {
log.Println(err)
}
}
Output:
What is your Name? Kiran
Entered Name is Kiran
Conclusion
In this post, You learned multiple ways to read single or multiple values using the scanner function. And also Bufio package is used to read input line by line with examples.