fmt.Fscanf() 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.Fscanf() function in Go language scans the specified text, read from r and then stores the successive space-separated values into successive arguments as determined by the format. Here newlines in the input must match newlines in 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 Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error)

Parameters: This function accepts three parameters which are illustrated below:

  • r io.Reader: This parameter contains the scanned specified texts.
  • format string: This parameter contains different formats for receiving the elements.
  • a …interface{}: This parameter is the specified variable for each elements.

Returns: It returns the number of items successfully parsed.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// fmt.Fscanf() function
  
// Including the main package
package main
  
// Importing fmt, io and strings
import (
    "fmt"
    "os"
    "strings"
)
  
// Calling main
func main() {
  
        // Declaring some type of variables
    var (
        i int
        b bool
        s string
    )
      
    // Calling the NewReader() function to
    // specify some type of texts.
    // variable "r" contains the scanned texts
    r := strings.NewReader("10 false GFG")
      
    // Calling the Fscanf() function to receive 
    // the scanned texts
    n, err := fmt.Fscanf(r, "%d %t %s", &i, &b, &s)
      
    // If the above function returns an error then
    // below statement will be executed
    if err != nil {
        fmt.Fprintf(os.Stderr, "Fscanf: %v\n", err)
    }
      
    // Printing each type of scanned texts
    fmt.Println(i, b, s)
      
    // It returns the number of items 
    // successfully scanned
    fmt.Println(n)
}

chevron_right


Output:

10 false GFG
3

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// fmt.Fscanf() function
  
// Including the main package
package main
  
// Importing fmt, io and strings
import (
    "fmt"
    "os"
    "strings"
)
  
// Calling main
func main() {
  
        // Declaring some type of variables
    var (
        i int
        b bool
        s string
        f float32
    )
      
    // Calling the NewReader() function to
    // specify some type of texts.
    // variable "r" contains the scanned texts
    r := strings.NewReader("46 true 3.4 GeeksforGeeks")
      
    // Calling the Fscanf() function to receive 
    // the scanned texts
    n, err := fmt.Fscanf(r, "%d %t %g %s", &i, &b, &f, &s)
      
    // If the above function returns an error then
    // below statement will be executed
    if err != nil {
        fmt.Fprintf(os.Stderr, "Fscanf: %v\n", err)
    }
      
    // Printing each type of scanned texts
    fmt.Println(i, b, f, s)
      
    // It returns the number of items 
    // successfully scanned
    fmt.Println(n)
}

chevron_right


Output:

46 true 3.4 GeeksforGeeks
4



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.