Golang fmt Fscanln Function

The fmt.Fscanln function in Golang is part of the fmt package and is used to scan and read input from an io.Reader, such as a file or buffer, until a newline is encountered. This function is useful for reading data line by line and storing it in variables.

Table of Contents

  1. Introduction
  2. Fscanln Function Syntax
  3. Examples
    • Basic Usage
    • Reading from a File
  4. Real-World Use Case
  5. Conclusion

Introduction

The fmt.Fscanln function reads input from an io.Reader until a newline character is reached. It is similar to fmt.Fscan, but it stops scanning at the end of the line. This is useful for processing input where each line contains separate pieces of data, such as reading CSV files or parsing text data.

Fscanln Function Syntax

The syntax for the fmt.Fscanln function is as follows:

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

Parameters:

  • r: An io.Reader from which the input is read.
  • a: Pointers to variables where the scanned data will be stored.

Returns:

  • n: The number of items successfully scanned and assigned.
  • err: An error if one occurred during scanning.

Examples

Basic Usage

This example demonstrates how to use the fmt.Fscanln function to read data from a string buffer.

Example

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Create a buffer with some data
	data := "Alice 30\nBob 25"
	buffer := bytes.NewBufferString(data)

	var name string
	var age int

	// Use fmt.Fscanln to read the first line from the buffer
	fmt.Fscanln(buffer, &name, &age)
	fmt.Printf("Name: %s, Age: %d\n", name, age)

	// Use fmt.Fscanln to read the second line from the buffer
	fmt.Fscanln(buffer, &name, &age)
	fmt.Printf("Name: %s, Age: %d\n", name, age)
}

Output:

Name: Alice, Age: 30
Name: Bob, Age: 25

Reading from a File

You can use fmt.Fscanln to read line-by-line data from a file.

Example

package main

import (
	"fmt"
	"os"
)

func main() {
	// Open a file for reading
	file, err := os.Open("data.txt")
	if err != nil {
		fmt.Println("Error opening file:", err)
		return
	}
	defer file.Close()

	var name string
	var age int

	// Use fmt.Fscanln to read data from each line of the file
	for {
		_, err := fmt.Fscanln(file, &name, &age)
		if err != nil {
			break
		}
		fmt.Printf("Name: %s, Age: %d\n", name, age)
	}
}

Contents of data.txt:

Charlie 40
Diana 35

Output:

Name: Charlie, Age: 40
Name: Diana, Age: 35

Real-World Use Case

Processing CSV Data

In real-world applications, fmt.Fscanln can be used to process CSV data line by line.

Example

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Simulate CSV data
	csvData := "product1,100\nproduct2,150\nproduct3,200"
	csvBuffer := bytes.NewBufferString(csvData)

	var product string
	var price int

	// Use fmt.Fscanln to read each line of CSV data
	for {
		_, err := fmt.Fscanln(csvBuffer, &product, &price)
		if err != nil {
			break
		}
		fmt.Printf("Product: %s, Price: %d\n", product, price)
	}
}

Output:

Product: product1, Price: 100
Product: product2, Price: 150
Product: product3, Price: 200

Conclusion

The fmt.Fscanln function is useful for reading input line by line from any io.Reader. It is ideal for applications that need to process data where each line contains separate pieces of information. By using fmt.Fscanln, you can efficiently read and parse line-separated data in your Go programs.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare