golang tutorial - Golang Read File | Reading Files In Golang - golang - go programming language - google go - go language - go program - google language
Reading and writing files are basic tasks needed for many Go programs. First we’ll look at some examples of reading files.
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
)
// Reading files requires checking most calls for errors. This helper will streamline our error checks below.
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
// Perhaps the most basic file reading task is slurping a file’s entire contents into memory.
dat, err := ioutil.ReadFile("/tmp/dat")
check(err)
fmt.Print(string(dat))
// You’ll often want more control over how and what parts of a file are read. For these tasks, start by Opening a file to obtain an os.File value.
f, err := os.Open("/tmp/dat")
check(err)
// Read some bytes from the beginning of the file. Allow up to 5 to be read but also note how many actually were read.
b1 := make([]byte, 5)
n1, err := f.Read(b1)
check(err)
fmt.Printf("%d bytes: %s\n", n1, string(b1))
// You can also Seek to a known location in the file and Readfrom there.
o2, err := f.Seek(6, 0)
check(err)
b2 := make([]byte, 2)
n2, err := f.Read(b2)
check(err)
fmt.Printf("%d bytes @ %d: %s\n", n2, o2, string(b2))
// The io package provides some functions that may be helpful for file reading. For example, reads like the ones above can be more robustly implemented with ReadAtLeast.
o3, err := f.Seek(6, 0)
check(err)
b3 := make([]byte, 2)
n3, err := io.ReadAtLeast(f, b3, 2)
check(err)
fmt.Printf("%d bytes @ %d: %s\n", n3, o3, string(b3))
// There is no built-in rewind, but Seek(0, 0) accomplishes this.
_, err = f.Seek(0, 0)
check(err)
// The bufio package implements a buffered reader that may be useful both for its efficiency with many small reads and because of the additional reading methods it provides.
r4 := bufio.NewReader(f)
b4, err := r4.Peek(5)
check(err)
fmt.Printf("5 bytes: %s\n", string(b4))
// Close the file when you’re done (usually this would be scheduled immediately after Opening with defer).
f.Close()
}
$ echo "hello" > /tmp/dat
$ echo "go" >> /tmp/dat
$ go run reading-files.go
click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language
output of the above go program
hello
go
5 bytes: hello
2 bytes @ 6: go
2 bytes @ 6: go
5 bytes: hello
Related Searches to Golang Read File | Reading Files In Golang
golang read file line by linegolang read file to stringgolang read json filegolang read csv filegolang io.reader examplegolang delete filegolang append to filegolang file existsgolang read filego readinggo io readergolang read from stdingolang file readergolang read file to stringgolang read http request bodygolang read csvgolang read files in directorygolang readgolanggoprogoogle gogolang tutorialgoogle languagego languagego programming languagego languagego programming languagegolang tutorialgo googlegoogle gogolanggp languagego programming language bookgolang coursethe go programming languagebest book to learn golanggolang web development tutorialgolang big datago language bookbig data golanggolang booklearn golanggolang programsgo programming language usesgo to google go to googlelearn go programminggoogle and gowhat is go programming languagego google go google go googlewhich programming language used in googlego to the bookgo scripting languagegolang online tutorialgoogle language programminggo language programminggolang iotgo to go to googlewhy go languagego programming language tutorialgo language tutoriallearn go languagegolang jobsgoogle go softwareprogramming language gogo programing languagegolang web frameworkgo for googlego language ide for windowsgoogle golanggolang programminggoogle coding languagegolang projectsgo programming tutorialgoogle go language tutorialgolang debuggergo to the googlegoogle go programming languagegolang classgolang serverprogramming language used by googlegoogle go languagego programminggo programming pdfgolang developmentgolang frameworkgo language examplesgolang tutorial pdflanguage googlego programgo programming language pdf