package main import ( "bufio" "fmt" "os" ) func main() { file, err := os.Open("input.txt") if err != nil { fmt.Println("Error opening file:", err) return } defer file.Close() 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 text: ") for scanner.Scan() { fmt.Println(scanner.Text()) fmt.Print("Enter more text: ") } }In this example, we create a scanner from stdin (the console). We then prompt the user to enter text and use the Scan method to read the input line by line and print it out to the console. In both examples, we use the bufio package library in Go and the Scanner Scan method to read input from different sources.