Home > Common Problem > How to modify files in golang

How to modify files in golang

尊渡假赌尊渡假赌尊渡假赌
Release: 2023-06-13 10:03:52
Original
1362 people have browsed it

The method for file modification in golang is: 1. Create a Go sample file; 2. Use the "os.OpenFile()" function to open the file to be modified and specify the opening method, permissions and other information; 3. Use "io.WriteString()" and other methods write data to the file; 4. After the modification is successful, use the "os.File.Sync()" function to synchronize the file content to the disk; 5. Call "file.Close()" Just close the file and print the results.

How to modify files in golang

Operating system for this tutorial: Windows 10 system, Go1.20.1 version, Dell G3 computer.

The method for file modification in golang is:

1. Use the os.OpenFile() function to open the file to be modified, and specify the opening method, permissions and other information.

For example:

file, err := os.OpenFile("example.txt", os.O_RDWR, 0644)
if err != nil {
    log.Fatal(err)
}
defer file.Close()
Copy after login

In the above example, we opened the file "example.txt", and used os.O_RDWR to indicate that it was opened in read-write mode, and finally specified the file permission as 0644.

2. Use methods such as io.WriteString(), io.Write() or fmt.Fprintf() to write data to the file.

For example:

if _, err := io.WriteString(file, "Hello, World!"); err != nil {
    log.Fatal(err)
}
Copy after login

Here, we use the io.WriteString() function to write the string "Hello, World!" to the file, and the function return value is the written bytes number, and an exception will be thrown if an error occurs.

3. After the modification is successful, use the os.File.Sync() function to synchronize the file contents to the disk. For example:

if err := file.Sync(); err != nil {
    log.Fatal(err)
}
Copy after login

After the above operations are completed, the file can be closed. So this example needs to call file.Close() at the end.

The following is a code example for completely modifying the file:

package main
import (
    "io"
    "log"
    "os"
)
func main() {
    f, err := os.OpenFile("example.txt", os.O_RDWR, 0644)
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()
    if _, err := io.WriteString(f, "Hello, World!"); err != nil {
        log.Fatal(err)
    }
    if err := f.Sync(); err != nil {
        log.Fatal(err)
    }
}
Copy after login

The output result is that the content of the file example.txt is "Hello, World!".

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

Related labels:
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