fmt.Scan() Function in Golang With Examples
In Go language, fmt package implements formatted I/O with functions analogous to C’s printf() and scanf() function. The fmt.Scan() 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. 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 Scan(a ...interface{}) (n int, err error)
Here, “a …interface{}” receives each type of the given texts.
Returns: It returns the number of items successfully scanned.
Example 1:
C
package main
import (
"fmt"
)
func main() {
var name string
var alphabet_count int
fmt.Scan(&name)
fmt.Scan(&alphabet_count)
fmt.Printf( "The word %s containing %d number of alphabets." ,
name, alphabet_count)
}
|
Input:
GFG 3
Output:
The word GFG containing 3 number of alphabets.
Example 2:
C
package main
import (
"fmt"
)
func main() {
var name string
var alphabet_count int
var float_value float32
var bool_value bool
fmt.Scan(&name)
fmt.Scan(&alphabet_count)
fmt.Scan(&float_value)
fmt.Scan(&bool_value)
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
Last Updated :
26 Feb, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...