fmt.Fscan() Function in Golang With Examples
Last Updated :
05 May, 2020
In Go language, fmt package implements formatted I/O with functions analogous to C’s printf() and scanf() function. The fmt.Fscan() function in Go language scans the specified text, read from r, and then stores the successive space-separated values into successive arguments. Here newlines get counted as space. 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 Fscan(r io.Reader, a ...interface{}) (n int, err error)
Parameters: This function accepts two parameters which are illustrated below:
- r io.Reader: This parameter contains the scanned specified texts.
- a …interface{}: This parameter receives each type of the specified texts.
Returns: It returns the number of items successfully scanned.
Example 1:
package main
import (
"fmt"
"os"
"strings"
)
func main() {
var (
i int
b bool
s string
)
r := strings.NewReader( "10 false GFG" )
n, err := fmt.Fscan(r, &i, &b, &s)
if err != nil {
fmt.Fprintf(os.Stderr, "Fscanf: %v\n" , err)
}
fmt.Println(i, b, s)
fmt.Println(n)
}
|
Output:
10 false GFG
3
Example 2:
package main
import (
"fmt"
"os"
"strings"
)
func main() {
var (
i int
b bool
s string
f float32
)
r := strings.NewReader( "46 true 3.4 GeeksforGeeks" )
n, err := fmt.Fscan(r, &i, &b, &f, &s)
if err != nil {
fmt.Fprintf(os.Stderr, "Fscanf: %v\n" , err)
}
fmt.Println(i, b, f, s)
fmt.Println(n)
}
|
Output:
46 true 3.4 GeeksforGeeks
4
Share your thoughts in the comments
Please Login to comment...