Details of golang defragmenter fmt.Scan

  • 2020-06-23 00:38:19
  • OfStack

This article introduced the Scan family of functions that get data from standard input from the fmt package in the golang language, the Fscan family of functions that get data from io.Reader, and the Sscan family of functions that get data from strings.

Scan series

go language fmt package contains fmt. Scan, fmt. Scanf, fmt. Scanln3 functions, which can obtain user input from standard input during program running.

fmt.Scan

grammar


func Scan(a ...interface{}) (n int, err error)

Scan scans text from standard input, reads values delimited by whitespace characters and saves them to arguments passed to this function, with newline characters treated as whitespace characters.

This function returns the number of successful scans and any errors encountered. If the number of readings is less than the parameters provided, an error reporting reason is returned.

Code sample


package main

import "fmt"
func main(){
 var (
  name string
  age int
  married bool
 )
 fmt.Scan(&name,&age,&married)
 fmt.Printf(" Scan results  name:%s age:%d married:%t\t",name,age,married)
}

Run the above code at the terminal and enter Alfred, 26, false in order, separated by Spaces.

[

go run main.go
alfred 26 false
Scan results name:alfred age:26 married:false

]

fmt.Scan scans user input from standard input and stores blank delimited data into specified parameters.

fmt.Scanf

grammar


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

Scanf scans text from standard input and reads whitespace-delimited values in the format specified by the format parameter to save to the parameters passed to this function.

This function returns the number of successful scans and any errors encountered.

The code examples


package main
import "fmt"
func main(){
var (
 name string
 age int
 married bool
)
fmt.Scanf("1:%s 2:%d 3:%t", &name,&age,&married)
fmt.Printf(" Scan results  name:%s age:%d married:%t", name,age,married)
}

Compile the above code and execute it at the terminal. Enter 1:ES83en 2:263:ES84en at the terminal once in the specified format.

[

go run main.go
1:alfred 2:26 3:false
Scan results name:alfred age:26 married:false

]

fmt.Scanf and ES103en.Scan are different from the simple space character as the delimiter of the input data. fmt.Scanf specifies the specific input content format for the input data, and only the input data in the format will be scanned and stored in the corresponding variable.

For example, fmt.Scanf will not scan the data correctly if we enter it as we did in the previous example, separated by Spaces.

fmt.Scanln

grammar


func Scanln(a ...interface{}) (n int, err error)

Scanln is similar to Scan in that it stops scanning when a newline is encountered. The last data must be followed by a line break or to the end position.

This function returns the number of successful scans and any errors encountered.

Code sample


package main
import "fmt"
func main(){
 var (
  name string
  age int
  married bool
 )
 fmt.Scanln(&name,&age,&married)
 fmt.Printf(" Scan results  name:%s age:%d married:%t",name,age,married)
}

The above code is compiled and executed at the terminal, where alfred 26 false is entered in turn, separated by Spaces.

[

go run main.go
alfred 26 false
Scan results name:alfred age:26 married:false

]

fmt.Scanln ends the scan when a carriage return is encountered.

Fscan series


func Fscan(r io.Reader, a ...interface{}) (n int, err error)
func Fscanln(r io.Reader, a ...interface{}) (n int, err error)
func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error)

These functions function like fmt.Scan, ES161en.Scanf, and fmt.es164EN3, except that instead of reading from standard input, they read from ES165en.Reader.

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 function like fmt.Scan, fmt.Scanf, and fmt.es179EN3, except that instead of reading from standard input, they read from a specified string.


Related articles: