awesome-go/impl/files-manipulation/16-ReadFromFile.go

31 lines
558 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"log"
"os"
)
func main() {
/*
Dosyadan n byte kadarlık akışı okuma (Read up to n Bytes from File)
*/
// Okumak için dosyayı
file, err := os.Open("demo.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
// len(b) kadar byte okuma yapılabilir.
// Dosya sonu io.EOF hata türünü döndürür.
byteSlice := make([]byte, 16)
bytesRead, err := file.Read(byteSlice)
if err != nil {
log.Fatal(err)
}
log.Printf("Number of bytes read: %d\n", bytesRead)
log.Printf("Data read: %s\n", byteSlice)
}