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 :
Advertisement
Something interesting
Tutorials
+9.2k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+10.1k Golang : Print how to use flag for your application example
+7.3k Golang : How to fix html/template : "somefile" is undefined error?
+32.7k Golang : Regular Expression for alphanumeric and underscore
+6.9k Nginx : Password protect a directory/folder
+9.3k Golang : How to get garbage collection data?
+9k Golang : Inject/embed Javascript before sending out to browser example
+7.7k Golang : Error reading timestamp with GORM or SQL driver
+12.3k Golang : Display list of countries and ISO codes
+8.8k Golang : Random integer with rand.Seed() within a given range
+23k Golang : Calculate time different
+17.7k Golang : Read data from config file and assign to variables