How to read files in Go language? A brief analysis of various methods
This article will discuss how to read files in Golang. We will use the following packages to process these files.
- os package provides a platform-independent interface to perform operation-level operations.
- IOutil The package provides easy-to-use utility functions for working with files without knowing much about the internal implementation.
- bufio package implements buffered IO, which helps us improve the performance and throughput of input and output operations.
-
log package implements a simple logging package. We will use this throughout the program. We will use the
Fatal()
function of the logging package in our program.
Whole file reading
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:
- Definition: Method declaration func
ReadFile(filename string) ([]byte, error)
- Function: The read path is passed into the function parameter file and returns the contents of the file. Internally used
os.ReadFile(fileName)
method - Returns: Returns the contents of the file and the error in a byte array. A successful call will return err == nil
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. - Then we put an if condition, if the err value is not nil, we will use the
log.Fatal()
function to print the error err. Remember, theFatal()
function is equivalent to thePrint()
function, which then callsos.Exit(1)
. - Finally, we will use the
fmt.Println()
function to print the contents of the file. We cannot simply print thecontent
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!Finally, execute the above code and you can see the following output:
$ go run main.go I Love Golang, This is One of the Best Languages on the World!
Press line Reading
Scanner Scanner provides a convenient interface for reading data, such as a file consisting of newline-delimited lines of text. It reads data through markers; theSplit 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) } }
- os.Open()
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.
We then use a scanner to read the file and scan line by line until the end of the file content. - Finally, we want to close the file.
Read word by word
We can also pass Read the file word by word: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) } }Run the code:
$ go run main.go I Love Golang, This is One of the Best Languages on the World!
Read the file in chunks
Read the entire file at once It seems like a simple method, but sometimes we need to make our program some optimizations from a memory management perspective. Golang provides a way to read a file in chunks instead of reading it as a whole or even line by line. Because if the size of a row is too large, reading row by row may also be inefficient.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() }
Binary reading
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!

The article discusses using Go's "strings" package for string manipulation, detailing common functions and best practices to enhance efficiency and handle Unicode effectively.

The article details using Go's "crypto" package for cryptographic operations, discussing key generation, management, and best practices for secure implementation.Character count: 159

The article details the use of Go's "time" package for handling dates, times, and time zones, including getting current time, creating specific times, parsing strings, and measuring elapsed time.

Article discusses using Go's "reflect" package for variable inspection and modification, highlighting methods and performance considerations.

The article discusses using Go's "sync/atomic" package for atomic operations in concurrent programming, detailing its benefits like preventing race conditions and improving performance.

The article discusses type conversions in Go, including syntax, safe conversion practices, common pitfalls, and learning resources. It emphasizes explicit type conversion and error handling.[159 characters]

The article discusses type assertions in Go, focusing on syntax, potential errors like panics and incorrect types, safe handling methods, and performance implications.

The article explains the use of the "select" statement in Go for handling multiple channel operations, its differences from the "switch" statement, and common use cases like handling multiple channels, implementing timeouts, non-b


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
