How to read input from console line?
Stdin used to read the data from command line.
After each line press "Enter" and stop writing press "Ctrl+C".
Example
package main
import (
"io"
"io/ioutil"
"log"
"fmt"
"os"
)
func main() {
fmt.Printf("Enter the text:\n")
writeText, err := os.Open(os.DevNull)
if err != nil {
log.Fatalf("failed to open a null device: %s", err)
}
defer writeText.Close()
io.WriteString(writeText,"Write Text")
readText, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("failed to read stdin: %s", err)
}
fmt.Printf("\nLength: %d", len(readText))
fmt.Printf("\nData Read: \n%s", readText)
}
Output
Enter the text:
Go
Python
Java
C++
C
Length: 26
Data Read:
Go
Python
Java
C++
C
Most Helpful This Week
Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]
URL parser in Golang
How to read/write from/to file in Golang?
Closures Functions in Golang
Copy an array by value and reference into another array
Subtract N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time.