golang scanf
时间: 2023-10-01 13:02:15 浏览: 93
As an AI language model, I do not have the capability to execute code directly. However, I can provide you with an explanation of how scanf works in golang.
In golang, the `fmt` package provides several functions for formatting input and output. `fmt.Scanf` is one of the functions for reading input from standard input (stdin) and storing it in variables.
The syntax of `fmt.Scanf` is as follows:
```
fmt.Scanf(format string, a ...interface{}) (n int, err error)
```
The first argument is a string that specifies the format of the input. The format string can contain placeholders for the input values. The placeholders are represented by verbs like `%d` for integers, `%f` for floats, `%s` for strings, etc.
The second argument is a variadic parameter that specifies the variables where the input values will be stored. The number and types of variables must match the placeholders in the format string.
For example, the following code reads two integers from stdin and stores them in variables `a` and `b`:
```
var a, b int
fmt.Scanf("%d %d", &a, &b)
```
The format string `"%d %d"` specifies two integers separated by a space. The variables `a` and `b` are passed to `fmt.Scanf` as pointers using the `&` operator.
If the input values do not match the format string or cannot be converted to the specified types, `fmt.Scanf` returns an error.
阅读全文