This article will discuss how to read files in Golang. We will use the following packages to process these files.
Fatal()
function of the logging package in our program. We can easily read the whole file in one go and store it in a variable. But remember, we shouldn't do this with large files. We will use the ioutil.ReadFile()
function to read a file and store the contents of the file in a variable.
First, let's store a file in the same directory as our program. Therefore, our folder structure will look like this.
___ | |_ _ _ _ ilovego.txt | |_ _ _ _ main.go
We write the following in the main.go
function:
package main import ( "fmt" "io/ioutil" "log" ) func main() { content, err := ioutil.ReadFile("ilovego.txt") if err != nil { log.Fatal(err) } fmt.Println(string(content)) }
But first, let’s discuss the ReadFile()
function Situation:
ReadFile(filename string) ([]byte, error)
os.ReadFile(fileName)
method The internal ReadFile function is implemented as follows:
func ReadFile(filename string) ([]byte, error) { return os.ReadFile(filename) }
Now, let’s return to our program:
ioutil.ReadFile("readthisfile.txt")
Returns a byteArray and an err. We store the byteArray in the "content" variable and the error in the "err" variable. log.Fatal()
function to print the error err. Remember, the Fatal()
function is equivalent to the Print()
function, which then calls os.Exit(1)
. fmt.Println()
function to print the contents of the file. We cannot simply print the content
variable because it is a byte array and we need to convert it to a string: string(content)
ilovego.txt file and write the following content:
I Love Golang, This is One of the Best Languages on the World!
$ go run main.go I Love Golang, This is One of the Best Languages on the World!
Split function defines markers. By default, this function breaks the data into rows and strips the row ends.
package main import ( "fmt" // "io/ioutil" "bufio" "log" "os" ) func main() { // opening the file using Open function f, err := os.Open("ilovego.txt") if err != nil { log.Fatal(err) } defer f.Close() // create a scanner for the file scanner := bufio.NewScanner(f) // loop through the scanner until it return fasle for scanner.Scan() { fmt.Println(scanner.Text()) } if err := scanner.Err(); err != nil { log.Fatal(err) } }
Open the file passed in the argument. If any error is encountered while opening the file, it will return the same error. Otherwise, the error will be nil.
package main import ( "bufio" "fmt" "os" ) func main() { f, err := os.Open("ilovego.txt") if err != nil { fmt.Println(err) } defer f.Close() scanner := bufio.NewScanner(f) scanner.Split(bufio.ScanWords) for scanner.Scan() { fmt.Println(scanner.Text()) } if err := scanner.Err(); err != nil { fmt.Println(err) } }
$ go run main.go I Love Golang, This is One of the Best Languages on the World!
package main import ( "bufio" "fmt" "io" "log" "os" ) func main() { f, err := os.Open("ilovego.txt") if err != nil { log.Fatal(err) } defer f.Close() reader := bufio.NewReader(f) buf := make([]byte, 16) for { // reading a file upto buffer n, err := reader.Read(buf) if err != nil { if err != io.EOF { log.Fatal(err) } break } fmt.Print(string(buf[0:n])) } fmt.Println() }
hex package implements hexadecimal encoding and decoding.
package main import ( "bufio" "encoding/hex" "fmt" "io" "log" "os" ) func main() { f, err := os.Open("sid.jpg") if err != nil { log.Fatal(err) } defer f.Close() reader := bufio.NewReader(f) buf := make([]byte, 256) for { _, err := reader.Read(buf) if err != nil { if err != io.EOF { fmt.Println(err) } break } fmt.Printf("%s", hex.Dump(buf)) } }
本文介绍了 Go 语言读取文件的几种方式,ioutil.ReadFile
函数将整个文件读成一个字符串。这个函数很方便,但不应该用于非常大的文件。希望能对你有所帮助!
推荐学习:Golang教程
The above is the detailed content of How to read files in Go language? A brief analysis of various methods. For more information, please follow other related articles on the PHP Chinese website!