In Go language, fmt package implements formatted I/O with functions analogous to C’s printf() and scanf() function. The fmt.Scanf() function in Go language scans the input texts which is given in the standard input, reads from there and stores the successive space-separated values into successive arguments as determined by the format. Moreover, this function is defined under the fmt package. Here, you need to import the “fmt” package in order to use these functions.
Syntax:
func Scanf(format string, a ...interface{}) (n int, err error)
Parameters: This function accepts two parameters which are illustrated below:
- format string: This is the different formats which are used for each given elements.
- a …interface{}: These parameters receive each given elements.
Returns: It returns the number of items successfully scanned.
Example 1:
// Golang program to illustrate the usage of // fmt.Scanf() function // Including the main package package main // Importing fmt import ( "fmt") // Calling main func main() { // Declaring some variables var name string var alphabet_count int // Calling Scanf() function for // scanning and reading the input // texts given in standard input fmt.Scanf("%s", &name;) fmt.Scanf("%d", &alphabet;_count) // Printing the given texts fmt.Printf("The word %s containg %d number of alphabets.", name, alphabet_count) } |
Input:
GFG 3
Output:
The word GFG containg 3 number of alphabets.
Example 2:
// Golang program to illustrate the usage of // fmt.Scanf() function // Including the main package package main // Importing fmt import ( "fmt") // Calling main func main() { // Declaring some variables var name string var alphabet_count int var float_value float32 var bool_value bool // Calling Scanf() function for // scanning and reading the input // texts given in standard input fmt.Scanf("%s", &name;) fmt.Scanf("%d", &alphabet;_count) fmt.Scanf("%g", &float;_value) fmt.Scanf("%t", &bool;_value) // Printing the given texts fmt.Printf("%s %d %g %t", name, alphabet_count, float_value, bool_value) } |
Input:
GeeksforGeeks 13 6.789 true
Output:
GeeksforGeeks 13 6.789 true
Recommended Posts:
- math.Y0() Function in Golang With Examples
- reflect.Int() Function in Golang with Examples
- io.MultiReader() Function in Golang with Examples
- io.TeeReader() Function in Golang with Examples
- io.LimitReader() Function in Golang with Examples
- string.Contains Function in Golang with Examples
- reflect.Len() Function in Golang with Examples
- fmt.Scanln() Function in Golang With Examples
- math.NaN() Function in Golang With Examples
- fmt.Scan() Function in Golang With Examples
- io.WriteString() Function in Golang with Examples
- io.CopyN() Function in Golang with Examples
- fmt.Printf() Function in Golang With Examples
- fmt.Println() Function in Golang With Examples
- fmt.Sprint() Function in Golang With Examples
- strings.Map() Function in Golang With Examples
- fmt.Fprintln() Function in Golang With Examples
- io.CopyBuffer() Function in Golang with Examples
- io.Copy() Function in Golang with Examples
- io.NewSectionReader() Function in Golang with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

