Use the fmt.Fprintf function to write formatted data to the specified Writer

王林
Release: 2023-07-26 09:34:55
Original
1577 people have browsed it

Use the fmt.Fprintf function to write formatted data to the specified Writer

In the Go language, the fmt package provides many functions for formatting output. Among them, the fmt.Fprintf function can write the formatted string into the specified Writer.

The fmt.Fprintf function is defined as follows:

func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
Copy after login

Among them, w represents an object that implements the io.Writer interface, the format parameter is a string format template, and a is a variable parameter , indicating the data that needs to be formatted.

Below, we introduce the use of this function through a simple example.

package main

import (
    "fmt"
    "os"
)

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{
        Name: "Tom",
        Age:  20,
    }

    file, err := os.Create("person.txt")
    if err != nil {
        fmt.Println("创建文件失败:", err)
        return
    }
    defer file.Close()

    // 使用fmt.Fprintf将格式化后的数据写入文件
    _, err = fmt.Fprintf(file, "姓名:%s
年龄:%d
", p.Name, p.Age)
    if err != nil {
        fmt.Println("写入文件失败:", err)
        return
    }

    fmt.Println("写入文件成功")
}
Copy after login

In this example, we define a Person structure, containing two fields: name and age. Then, we use the fmt.Fprintf function to write the formatted data to a file named person.txt.

In the function, a file object file is created through the os.Create function, and the file is delayed and closed through the defer keyword. Then, we wrote the formatted string into the file through the fmt.Fprintf function.

In this example, we use a format string, which uses the placeholders %s and %d. Among them, %s represents a placeholder of string type, and %d represents a placeholder of integer type. Through a formatting method similar to printf in C language, we can format the data into a string and write it to the specified file.

After running the program, we can see the following content in the person.txt file:

姓名:Tom
年龄:20
Copy after login

We can see that we successfully wrote the formatted data through the fmt.Fprintf function in the specified file. This method facilitates us to format the output of data, and flexibly choose to output to the console or a file, or even network streams and other objects that implement the io.Writer interface.

The above is the detailed content of Use the fmt.Fprintf function to write formatted data to the specified Writer. 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