ADVERTISEMENT
ADVERTISEMENT

Golang fmt.Fscanf() Function with Examples

Golang | fmt.Fscanf() Function: Here, we are going to learn about the Fscanf() function of the fmt package with its usages, syntax, and examples.
Submitted by IncludeHelp, on October 08, 2021

fmt.Fscanf()

In Go language, the fmt package implements formatted I/O with functions analogous to C's printf() and scanf(). The Fscanf() function is an inbuilt function of the fmt package which is used to scan the text read from r (io.Reader), and stores the successive space-separated values into the given successive arguments as determined by the format specifiers (format verbs). The newlines count as space.

It accepts three parameters (r io.Reader, format string, a ...interface{}) and returns the number of total items successfully scanned and an error if occurred during the read operation.

Syntax:

func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error)

Parameter(s):

  • r : The standard Input/Output.
  • format : String format with the format verbs.
  • a : A custom type that is used to specify a set of one or more method signatures, here we can provide a set of the variables, constants, functions, etc.

Return Value:

The return type of the fmt.Fscanf() function is (n int, err error), it returns the number of total items successfully scanned and an error if occurred during the read operation.

Example 1:

// Golang program to demonstrate the
// example of fmt.Fscanf() function

package main

import (
	"fmt"
	"strings"
)

func main() {
	var name string
	var age int

	// Function Fscanf() returns,
	// n: Number of items successfully scanned
	// err: Error if any
	n, err := fmt.Fscanf(strings.NewReader("Alexander 47"),
		"%s %d",
		&name, &age)

	// Printing variables, items scanned, err
	fmt.Println("Name :", name)
	fmt.Println("Age :", age)

	fmt.Println(n, "Items successfully scanned")
	fmt.Println("Error", err)
}

Output:

Name : Alexander
Age : 47
2 Items successfully scanned
Error <nil>

Example 2:

// Golang program to demonstrate the
// example of fmt.Fscanf() function

package main

import (
	"fmt"
	"strings"
)

func main() {
	var (
		name string
		roll int
		age  int
		perc float32
	)

	// Function Fscanf() returns,
	// n: Number of items successfully scanned
	// err: Error if any
	n, err := fmt.Fscanf(strings.NewReader("Alvin 101 47 87.50"),
		"%s %d %d %f",
		&name, &roll, &age, &perc)

	// Printing variables, items scanned, err
	fmt.Println("Name :", name)
	fmt.Println("Roll :", roll)
	fmt.Println("Age :", age)
	fmt.Println("Percentage :", perc)

	fmt.Println(n, "Items successfully scanned")
	fmt.Println("Error", err)
}

Output:

Name : Alvin
Roll : 101
Age : 47
Percentage : 87.5
4 Items successfully scanned
Error <nil>

Golang fmt Package »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.