Home >Backend Development >Golang >Use the fmt.Fscanf function to read formatted data from a string and assign it to a variable until the specified delimiter is encountered.
Use the fmt.Fscanf function to read formatted data from a string and assign it to a variable until the specified delimiter is encountered
In the fmt package of Go language, a series of input and Output functions, including the function fmt.Fscanf for formatting input. By using this function, you can read data in a specified format from a string and assign it to the corresponding variable.
First, let's look at a simple example.
package main import ( "fmt" "strings" ) func main() { str := "42 3.14 Go" var num int var pi float64 var lang string _, err := fmt.Fscanf(strings.NewReader(str), "%d %f %s", &num, &pi, &lang) if err != nil { fmt.Printf("格式化输入错误:%s ", err) return } fmt.Printf("num: %d ", num) fmt.Printf("pi: %f ", pi) fmt.Printf("lang: %s ", lang) }
In the above code, we define a string str, which contains an integer, a floating point number and a string, separated by spaces.
Next, we use the fmt.Fscanf function to read data from this string in the specified format and assign the result to the corresponding variable.
In this example, we defined three variables: num, pi and lang respectively to receive the read data. We enable fmt.Fscanf to store the read values into the corresponding variables by passing their memory addresses (using the & operator) as parameters.
%The format string specifies the data type we expect to read. In this example, %d represents an integer, %f represents a floating point number, and %s represents a string.
Notice that we use the strings.NewReader function to wrap the string str into an implementation of the io.Reader interface so that it can be passed to the fmt.Fscanf function. This is done because the fmt.Fscanf function requires a parameter of type io.Reader, and it can read data from any type that implements this interface.
Finally, we check whether the read is successful by checking the err variable. If err is not nil, a formatted input error has occurred.
If everything is OK, we can print out the read value.
In the above code, we use the fmt.Printf function to print the values of num, pi and lang. You can process these values according to your own needs.
To sum up, by using the fmt.Fscanf function, we can easily read formatted data from a string and assign it to the corresponding variable. This is useful for handling user input, parsing file data, etc. Different format strings can be used to specify different data types. It should be noted that when the specified delimiter is encountered during the reading process, reading will stop and the remaining data will be left in the input stream.
The above is the detailed content of Use the fmt.Fscanf function to read formatted data from a string and assign it to a variable until the specified delimiter is encountered.. For more information, please follow other related articles on the PHP Chinese website!