Home  >  Article  >  Backend Development  >  Use the fmt.Sscanf function to read formatted data from a string and assign it to a variable

Use the fmt.Sscanf function to read formatted data from a string and assign it to a variable

WBOY
WBOYOriginal
2023-07-25 19:49:47931browse

Use the fmt.Sscanf function to read formatted data from a string and assign it to a variable

In the Go language, the Sscanf function in the fmt package can read from a string very conveniently Formatted data and assign it to a variable. This function can parse the data in the string, convert the data to the corresponding type according to the specified format, and store the converted result in the specified variable.

The syntax of the fmt.Sscanf function is as follows:

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

Among them, str is the string to be parsed, format is the specified format, a is the variable list to be assigned, n is the number of successfully assigned variables, and err is the error description.

Below we use some code examples to deepen our understanding of the fmt.Sscanf function.

Example 1: Parse the integer in the string

package main

import (
    "fmt"
)

func main() {
    str := "I have 10 apples"
    var apples int
    n, err := fmt.Sscanf(str, "I have %d apples", &apples)
    if err != nil {
        fmt.Println("解析错误:", err)
        return
    }
    fmt.Println("解析成功,变量apples的值为:", apples)
    fmt.Println("成功赋值的变量个数:", n)
}

Output result:

The parsing is successful, the value of the variable apples is: 10
The number of successfully assigned variables : 1

In the above example, we parsed the string "I have 10 apples" through the fmt.Sscanf function, extracted the integer 10, and assigned it to the variable apples.

Example 2: Parse multiple values ​​​​in a string

package main

import (
    "fmt"
)

func main() {
    str := "I am 20 years old"
    var name string
    var age int
    n, err := fmt.Sscanf(str, "I am %s years old", &name, &age)
    if err != nil {
        fmt.Println("解析错误:", err)
        return
    }
    fmt.Println("解析成功,变量name的值为:", name)
    fmt.Println("解析成功,变量age的值为:", age)
    fmt.Println("成功赋值的变量个数:", n)
}

Output result:

Parse successfully, the value of variable name is: 20
Parse successfully, variable The value of age is: 0
The number of successfully assigned variables: 1

In example 2, we parse the "name" and "age" from the string "I am 20 years old" value. Although we specified two variables in the format string, since "20" is parsed as "name" of string type instead of "age" of integer type, the value of "age" is 0.

Summary:

Through the fmt.Sscanf function, we can easily parse the data in the specified format from the string and assign it to the corresponding variable. This is very useful in scenarios where string formatting is required such as configuration files and log files. But it should be noted that we must ensure that the parsed data type matches the variable type to avoid parsing errors.

The above is the detailed content of Use the fmt.Sscanf function to read formatted data from a string 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