This article introduces the usage of the Scan series functions that obtain data from standard input from the fmt package in the golang language, the Fscan series functions that obtain data from it, and the Scan series functions that obtain data from a string.
Scan series
There are three functions under the go language fmt package, which can obtain user input from standard input during the program operation.
grammar
func Scan(a ...interface{}) (n int, err error)
Scan scan text from standard input, read values separated by whitespace characters and save them into the parameters passed to this function. Line breaks are considered whitespace characters.
This function returns the number of data successfully scanned and any errors encountered. If the number of data reads is less than the provided parameters, an error report reason will be returned.
Code Example
package main import "fmt" func main(){ var ( name string age int married bool ) (&name,&age,&married) ("Scan result name:%s age:%d married:%t\t",name,age,married) }
Run the above code in the terminal, enter Alfred, 26, and false in sequence to separate it with spaces.
go run
alfred 26 false
Scan result name:alfred age:26 married:false
Scan the user input data from standard input and store the data separated by whitespace into the specified parameters separately.
grammar
func Scanf(format string, a ...interface{})(n int, err error)
Scanf scans text from standard input, reads the value separated by whitespace characters according to the format specified by the format parameter and saves it to the parameters passed to this function.
This function returns the number of data successfully scanned and any errors encountered.
Code example
package main import "fmt" func main(){ var ( name string age int married bool ) ("1:%s 2:%d 3:%t", &name,&age,&married) ("Scan result name:%s age:%d married:%t", name,age,married) }
After compiling the above code, execute it in the terminal, enter 1:alfred 2:26 3:false at one time in the terminal according to the specified format.
go run
1:alfred 2:26 3:false
Scan result name:alfred age:26 married:false
Unlike the separator that uses a simple space character as input data, a specific input content format is specified for the input data. Only when input data is input according to the format will it be scanned and stored in the corresponding variable.
For example, if we enter it in the space-separated way in the previous example, we cannot scan the input data correctly.
grammar
func Scanln(a ...interface{}) (n int, err error)
Scanln is similar to Scan, it stops scanning when it encounters a newline. The last data must be followed by a new line or the end position.
This function returns the number of data successfully scanned and any errors encountered.
Code Example
package main import "fmt" func main(){ var ( name string age int married bool ) (&name,&age,&married) ("Scan result name:%s age:%d married:%t",name,age,married) }
After compiling the above code, execute it in the terminal, enter alfred 26 false in sequence in the terminal and use space separation.
go run
alfred 26 false
Scan result name:alfred age:26 married:false
When you enter the car, you end the scan. This is more commonly used.
Fscan series
func Fscan(r , a ...interface{}) (n int, err error) func Fscanln(r , a ...interface{}) (n int, err error) func Fscanf(r , format string, a ...interface{}) (n int, err error)
These functions are similar to the three functions , , but they do not read data from standard input but from them.
Sscan series
func Sscan(str string, a ...interface{}) (n int, err error) func Sscanln(str string, a ...interface{}) (n int, err error) func Sscanf(str string, format string, a ...interface{}) (n int, err error)
These functions are similar to the three functions , , but they do not read data from standard input but from the specified string.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.