Home > Backend Development > Golang > Use the fmt.Fscanf function to read formatted data from the input and assign it to multiple variables until a newline character is encountered

Use the fmt.Fscanf function to read formatted data from the input and assign it to multiple variables until a newline character is encountered

WBOY
Release: 2023-07-25 11:09:15
Original
1633 people have browsed it

Use the fmt.Fscanf function to read formatted data from the input and assign it to multiple variables until a newline character is encountered

In the fmt package of Go language, there is a very practical function fmt.Fscanf can be used to read formatted input data and assign the read values ​​to multiple variables. The following describes how to use the fmt.Fscanf function, with code examples.

Before starting, you first need to understand the basic usage of the fmt.Fscanf function. Its function signature is as follows:

func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error)
Copy after login

The Fscanf function reads formatted data from the reader r. The format is specified by the format parameter. The read value is assigned to a...interface{} according to the format format. variables in, and returns the number of parameters n successfully read and the error message err.

The following is a simple code example that demonstrates how to use the fmt.Fscanf function to read formatted data from standard input and assign the read values ​​to multiple variables.

package main

import (
    "fmt"
    "os"
)

func main() {
    var name string
    var age int

    fmt.Println("请输入你的姓名和年龄(以空格分隔): ")

    _, err := fmt.Fscanf(os.Stdin, "%s %d
", &name, &age)
    if err != nil {
        fmt.Println("读取输入错误: ", err)
        return
    }

    fmt.Printf("你的姓名是 %s,年龄是 %d
", name, age)
}
Copy after login

In the above example, the variables name and age are first defined, which are used to store the read name and age respectively. Then use the fmt.Fscanf function to read formatted data from the standard input os.Stdin. "%s %d
" specifies the input format, "%s" means reading a string, and "%d" means Read an integer, "
" means read a space and a newline character. The read results are assigned to the two variables name and age in order. When reading, if an error is encountered, the err error message will be returned, and error handling is required.

Finally, print out the read result in the fmt.Printf function to verify whether the read result is correct.

Summary: Use the fmt.Fscanf function to easily read formatted data from the input and assign the read values ​​to multiple variables. By properly specifying the format, flexible reading and processing of input data can be achieved. It should be noted that error handling must be performed when reading to ensure the correctness of the reading operation.

I hope the code examples and explanations in this article can help you understand and use the fmt.Fscanf function.

The above is the detailed content of Use the fmt.Fscanf function to read formatted data from the input and assign it to multiple variables until a newline character is encountered. 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