Home > Backend Development > Golang > How to write files in golang

How to write files in golang

WBOY
Release: 2023-05-13 11:54:36
Original
2670 people have browsed it

Golang is a modern programming language whose flexible syntax and rich built-in libraries make it the language of choice for many developers. In Golang, reading and writing files is a common operation, such as writing logs, storing configuration file information, etc. This article will show you how to write a file writer using Golang.

  1. Open the file

In Golang, to write to a file, you first need to open the file. Use the OpenFile() function in the built-in package os to open the file.

file, err := os.OpenFile("test.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

                            if err != nil {
                                log.Fatal(err)
                            }
Copy after login

defer file.Close()

In the above code, the first parameter is the file name, the second parameter is the flag to open the file, and the third parameter is the permission of the file. If the file does not exist, it will be created automatically.

  1. Write data

After opening the file, you can write data to the file. Usually, we use the NewWriter() function in the built-in package bufio to create a new Writer object and use it to write data.

writer := bufio.NewWriter(file)

                        writer.WriteString("Hello, World!
Copy after login

")

                        writer.WriteString("Goodbye, World!
Copy after login

")

                        writer.Flush()
Copy after login

In the above code, we used the string " Hello, World!" and "Goodbye, World!" write data to the file. To ensure that the data is written to the file, the Writer's Flush() function must be called after the writing operation is completed.

  1. Close the file

After the file write operation is completed, the file must be closed to release the file handle and system resources. Usually, we use the defer statement to ensure that the file handle is closed after the program execution is completed.

defer file.Close()

  1. Complete code

The following is a complete Golang code example of a file writer:

package main
import (

"bufio"
"log"
"os"
Copy after login

)
func main() {

file, err := os.OpenFile("test.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
    log.Fatal(err)
}
defer file.Close()
writer := bufio.NewWriter(file)
writer.WriteString("Hello, World!
Copy after login

")

writer.WriteString("Goodbye, World!
Copy after login

")

writer.Flush()
Copy after login

}

Summary

In Golang, it is very simple to implement file writing. The file needs to be opened, data written and the file closed. In actual programming, we can also use more APIs and libraries to implement more complex file operations. Hope this article gave you an idea about how to write files in Golang.

The above is the detailed content of How to write files in golang. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template