package main import ( "bufio" "fmt" "os" ) func main() { file, err := os.Open("data.txt") defer file.Close() if err != nil { panic(err) } reader := bufio.NewReader(file) buffer := make([]byte, 1024) n, err := reader.Read(buffer) if err != nil { panic(err) } fmt.Printf("Read %d bytes: %s\n", n, buffer) }
package main import ( "bufio" "fmt" "os" ) func main() { reader := bufio.NewReader(os.Stdin) fmt.Print("Enter text: ") line, err := reader.ReadString('\n') if err != nil { panic(err) } fmt.Println("Input:", line) }Both of the above examples use the bufio package in Go. The package library is bufio.