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
+12.7k Golang : Remove or trim extra comma from CSV
+6.8k Golang : Find the longest line of text example
+6.9k How to let Facebook Login button redirect to a particular URL ?
+22.2k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+9.9k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+13.7k Golang : Check if an integer is negative or positive
+4.6k MariaDB/MySQL : How to get version information
+36.5k Golang : Save image to PNG, JPEG or GIF format.
+11.3k Golang : How to use if, eq and print properly in html template
+6.4k Golang : How to search a list of records or data structures
+5.6k Golang : Detect words using using consecutive letters in a given string
+18.5k Golang : Aligning strings to right, left and center with fill example