


Use the fmt.Fprintf function to write formatted data to the specified Writer
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)
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("写入文件成功") }
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
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!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



DOS command is a command line tool used in Windows operating system, which can be used to perform various system management tasks and operations. One of the common tasks is to format the hard drive, including the C drive. Formatting the C drive is a relatively dangerous operation because it will erase all data on the C drive and reinitialize the file system. Before performing this operation, make sure you have backed up important files and have a clear understanding of the impact that formatting will have on your computer. The following is formatted in the DOS command line

How to optimize the performance of time and date formatting and parsing in Java development Summary: In Java development, time and date formatting and parsing are common operations. However, due to the complexity and variety of time and date formats and the huge amount of data processed, it often becomes a performance bottleneck. This article will introduce several methods to optimize the performance of time and date formatting parsing in Java development, including using cache, reducing object creation, selecting appropriate APIs, etc. 1. Introduction Time and date formatting and parsing are very common in Java development. However, in practical applications, since

The reasons why the D drive cannot be formatted include that the drive is being used by other programs or processes, there is a damaged file system on the drive, hard disk failure and permission issues. Detailed introduction: 1. The reason why the D drive cannot be formatted may be because the drive is being used by other programs or processes. In the Windows operating system, if a program is accessing the files or folders on the D drive, the system will not be able to perform the format operation. ;2. The reason why the D drive cannot be formatted may be because there is a damaged file system on the drive. The file system is used by the operating system to organize and manage files and folders on the storage device, etc.

Disc formatting refers to the process of rebuilding and clearing the disc's file system. During the disc formatting process, all data will be completely deleted, and the file system will be re-established to re-store data on the disc. Disc formatting can be used to protect data security, repair disc failures, and remove viruses. When formatting a disc, you need to back up important data, select an appropriate file system, and wait patiently for the formatting to complete.

Nowadays, we will inevitably encounter some problems such as being unable to turn on the phone or lagging, such as system crash, but during use, mobile phones have become an indispensable part of our lives. We are often at a loss, and sometimes, there are no solutions to these problems. To help you solve cell phone problems, this article will introduce you to some methods of cell phone format recovery and restore your phone to normal operation. Back up data - protect important information, such as photos and contacts, from being lost during the formatting process. Before formatting your phone, the first thing to consider is to back up important data and files on your phone. To ensure data security, or choose to transfer files to a cloud storage service, you can back it up by connecting to a computer. Use the system's built-in recovery function - simple

Many times when using a computer, you will encounter too much garbage, but many users still don’t know how to format the computer. It doesn’t matter. Here is a tutorial on computer formatting for you to take a look at. How to format a computer: 1. Right-click "This PC" on the desktop and click "Manage". 2. Click "Storage" in "Computer Management" to open "Disk Management". 3. Select the hard drive you want to clean, right-click and select "Format". 4. Check "Perform Quick Format" and click "OK" to start formatting.

Will formatting a laptop make it faster? If you want to format your Windows laptop but want to know if it will make it faster, this article will help you know the right answer to this question. Will formatting a laptop make it faster? There are many reasons why users format their Windows laptops. But the most common reason is slow performance or speed of your laptop. Formatting a laptop will completely delete all data stored on the C drive or the hard drive partition where Windows operating system is installed. Therefore, every user will think twice before taking this step, especially when it comes to the performance of the laptop. This article will help you understand whether formatting your laptop will speed it up. Formatting your laptop helps

Use the fmt.Sprint function to format multiple values into strings and return them, including type information. In the Go language, the fmt package provides many functions for formatting data into strings. Among them, the fmt.Sprint function can format multiple values into strings and return them. Unlike the fmt.Sprintf function, the fmt.Sprint function returns a string instead of a formatted string. Here is a simple example code using the fmt.Sprint function: pa
