Golang fmt.Fscanf() function examples

package fmt

Fscanf scans text read from r(1st parameter), storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully parsed.

Golang fmt.Fscanf() function usage examples

Example 1:

 func readCon(name string) (string, error) {
 var val string
 in, err := os.Open(name)
 if err != nil {
 return "", err
 }
 defer in.Close()

 _, err = fmt.Fscanf(in, "%s", &val) // <-- here
 return val, err
 }

Example 2 :

 team := os.Args[0]

 var answer string
 fmt.Fprintf(os.Stdout, `Are you sure you want to remove team "%s"? (y/n) `, team)
 fmt.Fscanf(os.Stdin, "%s", &answer) // <-- here
 if answer != "y" {
 fmt.Fprintln(os.Stdout, "Abort.")
 return nil
 }

Reference :

http://golang.org/pkg/fmt/#Fscanf

Advertisement