Home >Backend Development >Golang >Use the fmt.Scanf function to read formatted data from the input and assign it to a variable

Use the fmt.Scanf function to read formatted data from the input and assign it to a variable

PHPz
PHPzOriginal
2023-07-24 20:01:561368browse

Use the fmt.Scanf function to read formatted data from the input and assign it to the variable

In the Go language, we often need to read the data entered by the user from the standard input and assign it to the variable. Assign a value to a variable. The Scanf function in the fmt package can help us achieve this function. This article will introduce how to use the Scanf function to read formatted data from the input and assign it to a variable.

First, let us take a look at the basic syntax of the Scanf function:

func Scanf(format string, a ...interface{}) (n int, err error)

Among them, format is a string used to specify the format of the input data, and a is a series of values ​​to be assigned. variable. The...interface{} here indicates that a can be one or more variables of different types. The function will read data from the input in turn and assign it to the corresponding variable in a.

Next, we will demonstrate how to use the Scanf function through some specific examples.

Example 1: Reading an integer

Suppose we need to read an integer from the input and assign it to the variable num. The code is as follows:

package main

import "fmt"

func main() {
    var num int
    fmt.Println("请输入一个整数:")
    fmt.Scanf("%d", &num)
    fmt.Printf("您输入的整数是:%d
", num)
}

After running this program, "Please enter an integer:" will be output, and then wait for the user to input at the terminal. After the user enters the complete number and presses the Enter key, the program will read the integer and assign it to the num variable, and print it out.

Example 2: Reading multiple data

Suppose we need to read a string and an integer from the input and assign them to the variables name and age. The code is as follows:

package main

import "fmt"

func main() {
    var name string
    var age int
    fmt.Println("请输入您的姓名和年龄:")
    fmt.Scanf("%s %d", &name, &age)
    fmt.Printf("您的姓名是:%s,年龄是:%d
", name, age)
}

After running the program, "Please enter your name and age:" will be output, and then wait for the user to input in the terminal. After the user enters their name and age and presses the Enter key, the program will read the entered data, assign it to the name and age variables, and print it out.

It should be noted that for different data types, different placeholders are used in the format string. For example, %d represents an integer, %s represents a string, %f represents a floating point number, etc. The order of placeholders in the format string must match the order of the variables to be assigned.

Summary:

This article introduces the method of using the Scanf function in the fmt package to read formatted data from the input and assign it to a variable. In this way, we can easily obtain the required data from the user's input and perform subsequent processing in the program. You can flexibly use the Scanf function to read different types of data according to your actual needs and perform corresponding operations.

The above is the detailed content of Use the fmt.Scanf function to read formatted data from the input and assign it to a variable. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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