fmt.Fscanln() 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.Fscanln() function in Go language scans the specified text, read from r and then stores successive space-separated values into successive arguments. This function stops scanning at a newline and after the final item, there must be a newline or EOF. 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 Fscanln(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{}: These parameters are accepting each specified elements.

Returns: It returns the number of items successfully scanned.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// fmt.Fscanln() function
  
// Including the main package
package main
  
// Importing fmt, io and strings
import (
    "fmt"
    "io"
    "strings"
)
  
// Calling main
func main() {
  
    // Declaring list of strings,
    // integers and float value
    s := `gfg 9`
  
    // Calling NewReader() function for
    // reading each elements of the list
    // and then it place it into "r"
    r := strings.NewReader(s)
  
    // Declaring different types of variables
    var a string
    var b int
  
    for {
  
        // Calling Fscanln() function
        n, err := fmt.Fscanln(r, &a, &b)
  
        // Checking returned error is
        // end of the line (EOF) or not
        if err == io.EOF {
            break
        }
  
        // Checking if there is any error
        if err != nil {
            panic(err)
        }
  
        // Printing the number of items successfully
        // scanned and each elements too
        fmt.Printf("%d: %s, %d", n, a, b)
    }
}

chevron_right


Output:

2: gfg, 9

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// fmt.Fscanln() function
  
// Including the main package
package main
  
// Importing fmt, io and strings
import (
    "fmt"
    "io"
    "strings"
)
  
// Calling main
func main() {
  
    // Declaring list of strings,
    // integers and float value
    s := `gfg 9 true 5.78`
  
    // Calling NewReader() function for
    // reading each elements of the list
    // and then it place it into "r"
    r := strings.NewReader(s)
  
    // Declaring different types of variables
    var a string
    var b int
    var c bool
    var d float32
  
    for {
  
        // Calling Fscanln() function
        n, err := fmt.Fscanln(r, &a, &b, &c, &d)
  
        // Checking returned error is
        // end of the line (EOF) or not
        if err == io.EOF {
            break
        }
  
        // Checking if there is any error
        if err != nil {
            panic(err)
        }
  
        // Printing the number of items successfully
        // scanned and each elements too
        fmt.Printf("%d: %s, %d, %t, %g", n, a, b, c, d)
    }
}

chevron_right


Output:

4: gfg, 9, true, 5.78



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.