{

Golang Program show value by reading user input from console


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

Example to read user input using the scan function

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 an string  
 var name string  
 fmt.Print("What is your Name? ")  
 fmt.Scan(&name)  
 fmt.Println("Entered Name is :", name)  
  
}  

Output is

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 is

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.

Example program to read user input line using stdin stream

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 is

What is your Name? Kiran  
Entered Name is Kiran  

Conclusion

In this post, You learned multiple ways to read the single or multiple values using the scanner function. And also Bufio package is used to read input line by line with examples.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.