Home > Backend Development > Golang > Use the fmt.Fprintf function to write formatted data to the specified file. If the file does not exist, create it and return the number of bytes written and error information.

Use the fmt.Fprintf function to write formatted data to the specified file. If the file does not exist, create it and return the number of bytes written and error information.

PHPz
Release: 2023-07-24 10:21:22
Original
1224 people have browsed it

Use the fmt.Fprintf function to write formatted data to the specified file. If the file does not exist, create it and return the number of bytes written and the error message

In the Go language, we use fmt .Fprintf function to write formatted data to the specified file. This function will write data to the file in the specified format. If the file does not exist, it will be automatically created and return the number of bytes written and error information.

Here is a sample code that shows how to use the fmt.Fprintf function to write data to a file:

package main

import (
    "fmt"
    "os"
)

func main() {
    fileName := "output.txt"

    // 打开或创建文件
    file, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0755)
    if err != nil {
        fmt.Printf("打开文件失败:%s
", err.Error())
        return
    }
    defer file.Close()

    // 将数据写入文件
    count, err := fmt.Fprintf(file, "Hello World!
This is a sample text!")
    if err != nil {
        fmt.Printf("写入文件失败:%s
", err.Error())
        return
    }

    fmt.Printf("成功写入%d个字节
", count)
}
Copy after login

In the above code, we first define the file name to be written is "output.txt". Then open or create the file through the os.OpenFile function, use the os.O_RDWR | os.O_CREATE flag to specify to open the file in read-write mode, and create the file if it does not exist.

Next, we call the fmt.Fprintf function to write the string "Hello World!
This is a sample text!" into the file. The first parameter of the function is the target file to be written, and the second parameter is the data to be written. The first value returned by the function is the number of bytes written, and the second value is a possible error message.

Finally, we print out the number of bytes successfully written through the fmt.Printf function.

It should be noted that when using the fmt.Fprintf function, the file must be opened in a writable mode, that is, the os.O_RDWR flag must be specified, otherwise the file cannot be written.

Through the above sample code, we can see how to use the fmt.Fprintf function to write formatted data to a specified file and automatically create it if the file does not exist. At the same time, we can also judge whether the write is successful based on the number of bytes and error information returned by the function. This way we can handle file writing operations more flexibly.

The above is the detailed content of Use the fmt.Fprintf function to write formatted data to the specified file. If the file does not exist, create it and return the number of bytes written and error information.. 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