Read formatted data from input using the fmt.Fscanf function
Use the fmt.Fscanf function to read formatted data from the input
In the Go language, the fmt package provides multiple functions for formatting input and output. Among them, the fmt.Fscanf function can read data from the input in the specified format.
The following is a sample code that demonstrates how to use the fmt.Fscanf function to read formatted data from standard input:
package main import ( "fmt" "os" ) func main() { var name string var age int fmt.Print("请输入姓名和年龄(以空格分隔):") // 从标准输入中读取格式化的数据 _, err := fmt.Fscanf(os.Stdin, "%s %d", &name, &age) if err != nil { fmt.Println("读取数据失败:", err) return } fmt.Println("姓名:", name) fmt.Println("年龄:", age) }
In the above code, two variables name are first declared and age, used to store data read from the input. Then, use the fmt.Print function to print prompt information, prompting the user to enter their name and age. Then, use the fmt.Fscanf function to read data from the standard input, and use the format string "%s %d" to specify the input format, where "%s" means reading a string and "%d" means Read an integer. Note that you need to use the address operator "&" to pass the address of the variable to the fmt.Fscanf function.
If the data is read successfully, the returned result is the number of successfully read parameters and nil error. We use the "_" placeholder in the code to ignore the number of parameters returned and only care about the error. If reading the data fails, a non-nil error is returned.
Finally, use the fmt.Println function to print out the read name and age.
When running the above code, the console will print a prompt message and wait for the user to enter their name and age. After the user completes the input, press the Enter key, and the program will read the entered data and print out the name and age.
To summarize, by using the fmt.Fscanf function to read formatted data from the input, we can easily obtain user input and parse and process it according to the specified format. This gives us greater flexibility and convenience in handling input.
The above is the detailed content of Read formatted data from input using the fmt.Fscanf function. 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



Recently, many Win11 users have encountered the problem that the input experience dialog box always flickers and cannot be turned off. This is actually caused by the default system services and components of Win11. We need to disable the relevant services first, and then disable the input experience service. Solved, let’s try it out together. How to turn off the input experience in win11: First step, right-click the start menu and open "Task Manager". Second step, find the three processes "CTF Loader", "MicrosoftIME" and "Service Host: Textinput Management Service" in order, right-click "End Task" "The third step, open the start menu, search and open "Services" at the top. The fourth step, find "Textinp" in it
![Windows input encounters hang or high memory usage [Fix]](https://img.php.cn/upload/article/000/887/227/170835409686241.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
The Windows input experience is a key system service responsible for processing user input from various human interface devices. It starts automatically at system startup and runs in the background. However, sometimes this service may automatically hang or occupy too much memory, resulting in reduced system performance. Therefore, it is crucial to monitor and manage this process in a timely manner to ensure system efficiency and stability. In this article, we will share how to fix issues where the Windows input experience hangs or causes high memory usage. The Windows Input Experience Service does not have a user interface, but it is closely related to handling basic system tasks and functions related to input devices. Its role is to help the Windows system understand every input entered by the user.

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

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.

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.

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

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

How to solve 0x80070057 error: simple and effective methods and steps Introduction: In the process of using the computer, we sometimes encounter various error codes. Among them, 0x80070057 is a very common error code, which is usually related to Windows operating system. This error code can appear in different situations, such as when installing or updating the operating system, backing up or restoring files, formatting drives, etc. Although this error code is frustrating, it's not unsolvable. This article will introduce
