file, err := os.Open("somefile.txt") if err != nil { log.Fatal(err) } defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { fmt.Println(scanner.Text()) } if err := scanner.Err(); err != nil { log.Fatal(err) }
file, err := os.Open("somefile.bin") if err != nil { log.Fatal(err) } defer file.Close() buffer := make([]byte, 8) // read 8 bytes _, err := file.Read(buffer) if err != nil { log.Fatal(err) } fmt.Printf("%x", buffer) // print raw bytes in hexadecimal formatIn both examples, bufio.Reader is used to provide buffered input reading.