How to read standard input from console in Go
Go, commonly referred to as Golang, is a statically typed, compiled programming language developed by Google. Known for its simplicity, efficiency, and strong support for concurrent programming, Go is designed to be fast, readable, and suitable for a wide range of applications, from system-level programming to web development.
Reading input from console
Reading standard input from the console allows programs to interact with users in real-time. It enables users to provide data, instructions, or responses to a program during its execution. This input can be used for various purposes such as configuring settings, providing input data for calculations, making decisions based on user choices, and creating interactive command-line applications.
To read input from the console in Go, you can utilize packages like bufio or fmt. The bufio package provides a flexible approach, enabling line-by-line input using a Scanner and is useful for more complex scenarios. Alternatively, the fmt package offers concise functions like Scan or Scanln to directly read input.
Using bufio package
In Go, you can read from the standard input (console) using the bufio package, which provides a convenient way to read input line by line. Here's a simple example demonstrating how to read input from the console:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
// Create a new scanner to read from standard input
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Enter some text (press Ctrl+D or Ctrl+Z to end):")
// Read input line by line
for scanner.Scan() {
text := scanner.Text() // Get the current line of text
if text == "" {
break // Exit loop if an empty line is entered
}
fmt.Println("You entered:", text)
}
if err := scanner.Err(); err != nil {
fmt.Println("Error:", err)
}
}Using fmt package
fmt.Scan is a function provided by the fmt package in Go that allows you to read data from standard input (console). It takes a variable number of arguments, each of which should be a pointer to a variable where the input value will be stored. This function reads space-separated values from a single line of input and assigns them to the specified variables. It's useful when you expect input values to be separated by spaces or when you want to read a single value. Here's an example using fmt.Scan:
package main
import "fmt"
func main() {
fmt.Println("Enter some text:")
var input string
// Read a single line of input
_, err := fmt.Scan(&input)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("You entered:", input)
} fmt.Scanln is another function provided by the fmt package. It reads a full line of input from standard input and assigns the values to the specified variables. Unlike fmt.Scan, fmt.Scanln reads the entire line until a newline character is encountered. This is useful for reading multiple values entered on the same line or reading strings that might contain spaces.
package main
import "fmt"
func main() {
fmt.Println("Enter multiple lines of text (press Ctrl+D or Ctrl+Z to end):")
var input string
for {
_, err := fmt.Scanln(&input)
if err != nil {
break
}
fmt.Println("You entered:", input)
}
}Conclusion
In Go, reading standard input from the console is a technique that helps developers to create interactive command-line applications. The bufio package provides a versatile approach for line-by-line input, while the fmt package's Scan and Scanln functions offer convenient methods to capture user input.
Free Resources