Golang fmt.Fscan() function examples

package flag

Fscan scans text read from r(1st parameter), storing successive space-separated values into successive arguments. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why.

Golang fmt.Fscan() function usage examples

Example 1:

 func promptForString(field string, r io.Reader) string {
  fmt.Printf("Please enter %s: ", field)
  var result string
  fmt.Fscan(r, &result)
  return result
 }

Example 2:

 func KeysFromFile(name string) (string, string, error) {
  file, err := os.Open(name)
  if err != nil {
 return "", "", err
  }
  defer file.Close()

  var secret, access string
  _, err = fmt.Fscan(file, &secret, &access) // <-- here
  return secret, access, err
 }

Reference :

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

Advertisement