The fmt.Fscan
function in Golang is part of the fmt
package and is used for scanning and reading input from an io.Reader
, such as a file or a buffer. This function parses the input data according to the format of its arguments and stores the parsed values in the provided variables.
Table of Contents
- Introduction
Fscan
Function Syntax- Examples
- Basic Usage
- Reading from a File
- Real-World Use Case
- Conclusion
Introduction
The fmt.Fscan
function reads formatted input from an io.Reader
. It is similar to fmt.Scan
, but instead of reading from the standard input, it reads from a specified reader. This is useful when processing input from files, network connections, or other sources that implement the io.Reader
interface.
Fscan Function Syntax
The syntax for the fmt.Fscan
function is as follows:
func Fscan(r io.Reader, a ...interface{}) (n int, err error)
Parameters:
r
: Anio.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.Fscan
function to read data from a string buffer.
Example
package main
import (
"bytes"
"fmt"
)
func main() {
// Create a buffer with some data
data := "John 25"
buffer := bytes.NewBufferString(data)
var name string
var age int
// Use fmt.Fscan to read data from the buffer
fmt.Fscan(buffer, &name, &age)
// Print the scanned values
fmt.Printf("Name: %s, Age: %d\n", name, age)
}
Output:
Name: John, Age: 25
Reading from a File
You can also use fmt.Fscan
to read formatted 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 item string
var price float64
// Use fmt.Fscan to read data from the file
_, err = fmt.Fscan(file, &item, &price)
if err != nil {
fmt.Println("Error reading from file:", err)
return
}
// Print the scanned values
fmt.Printf("Item: %s, Price: %.2f\n", item, price)
}
Contents of data.txt
:
Laptop 1299.99
Output:
Item: Laptop, Price: 1299.99
Real-World Use Case
Reading Configuration Files
In real-world applications, fmt.Fscan
can be used to read configuration settings from a file or input stream.
Example
package main
import (
"fmt"
"os"
)
func main() {
// Simulate a configuration file with key-value pairs
configData := "host localhost\nport 8080"
configFile := bytes.NewBufferString(configData)
var key1, value1, key2 string
var value2 int
// Use fmt.Fscan to read configuration settings
fmt.Fscan(configFile, &key1, &value1, &key2, &value2)
// Print the configuration settings
fmt.Printf("%s: %s\n%s: %d\n", key1, value1, key2, value2)
}
Output:
host: localhost
port: 8080
Conclusion
The fmt.Fscan
function is useful for reading and parsing formatted input from any io.Reader
. It is ideal for applications that need to process structured data from files, buffers, or network connections. By using fmt.Fscan
, you can efficiently extract data from various sources in your Go programs.
Comments
Post a Comment
Leave Comment