Instructions for fmt.Scan in Go language

In go language, if you want to get user input, you will use scan method. There are many ways to use scan in the go language. Today I will introduce their usage and differences.

Like print, scan falls into three categories:

  • Scan, Scanf, and Scanln: read text from standard input os.Stdin (get data from terminal)
  • Fscan, Fscanf, Fscanln: read text from the specified io.Reader interface (general)
  • Scan, scan, scan: read text from a parameter string (get data from string)

Scan:

grammar: func Scan(a ...interface{}) (n int, err error)
//Example:
func main() {
          var str string
          fmt.Printf("Please enter content:")
          fmt.Scan(&str)
          fmt.Printf("str: %s",str)
}
/* scan Assign values to variables before the first space or line break is encountered. If
scan There are multiple variables in, and the values of the variables are separated by spaces or line breaks. So wrap and empty
 A lattice cannot be stored in a variable.
*/

Scanf:

Syntax: func Scanf(format string, a ...interface{}) (n int, err error)
//Example:
func main() {
    var (
        name string
        age int
    )   
    fmt.Printf("Please enter content:")
    fmt.Scanf("name:%s age:%d",&name,&age)
    fmt.Printf("n: %s, a:%d",name,age)
}   

Scanln:

Syntax: func Scanln(a ...interface{}) (n int, err error)
//Example:
func main() {
    var str string
    fmt.Printf("Please enter content:")
    fmt.Scanln(&str)
    fmt.Printf("str: %s",str)
}       
//Scanln is similar to Scan, but stops scanning immediately when a line break is encountered.

Summary: scan and Scanln are basically the same. The only difference is that when reading multiple variables, Scanln will end directly when encountering a line feed character, and the variable that does not read the input value is zero. Scan will wait until the input value meets the number of parameters before encountering a line feed character.

Sscan,Sscanf,Sscanln

grammar
func Sscan(str string, a ...interface{}) (n int, err error)
func Sscanf(str string, format string, a ...interface{}) (n int, err error)
func Sscanln(str string, a ...interface{}) (n int, err error)
//Example:
func main() {
    var i1,i2 int
    fmt.Sscan("100\n200",&i1,&i2)
    fmt.Printf("int1: %d,int2: %d", i1,i2)
}
//The usage is basically the same as scan, but it is changed to get data from string

Fscan,Fscanf,Fscanln:

func Fscan(r io.Reader, a ...interface{}) (n int, err error)
func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error)
func Fscanln(r io.Reader, a ...interface{}) (n int, err error)
/*
This method can be used as long as the object implements the read method (satisfying the io.Reader interface). Scan,Scanf,Scanln and sscan, scanf and scanln mentioned above are all encapsulated by this method.
The use method is the same as above, except that you need to specify one more source to implement the read method.
*/

Because the above methods use spaces as separators, they cannot get data with spaces. If there is a need for this, bufio can be used. As follows:

func main() {
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Input information:")
    input, _ := reader.ReadString('\n')
    fmt.Printf("n: %s",input)
}
//This will assign all the data before the line break to input

Keywords: Go

Added by Tux-e-do on Mon, 30 Mar 2020 17:05:17 +0300