package main import ( "bufio" "fmt" "os" ) func main() { file, err := os.Open("file.txt") if err != nil { fmt.Println("Error:", err) return } scanner := bufio.NewScanner(file) for scanner.Scan() { fmt.Println(scanner.Text()) } }
package main import ( "bufio" "fmt" "os" ) func main() { scanner := bufio.NewScanner(os.Stdin) fmt.Print("Enter your name: ") scanner.Scan() name := scanner.Text() fmt.Println("Hello,", name) }In both examples, we import the bufio package, create a Scanner using bufio.NewScanner(), and use the scanner.Scan() method to read the input data. In the first example, we read from a file, while in the second example, we read from the command line (stdin).