🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The fmt.Fscanf function in Golang is part of the fmt package and is used to scan and read formatted input from an io.Reader, such as a file or a buffer, based on a specified format string. This function allows you to parse input data according to format specifiers and store the parsed values in provided variables.
Table of Contents
- Introduction
FscanfFunction Syntax- Examples
- Basic Usage
- Reading from a File
- Real-World Use Case
- Conclusion
Introduction
The fmt.Fscanf function reads formatted input from an io.Reader using a format string, which makes it more flexible and precise than fmt.Fscan. It is particularly useful for parsing structured data from files, network connections, or other sources that implement the io.Reader interface.
Fscanf Function Syntax
The syntax for the fmt.Fscanf function is as follows:
func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error)
Parameters:
r: Anio.Readerfrom which the input is read.format: A format string containing format specifiers that determine how to parse the input.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.Fscanf function to read formatted data from a string buffer.
Example
package main
import (
"bytes"
"fmt"
)
func main() {
// Create a buffer with some formatted data
data := "Name: John Age: 25"
buffer := bytes.NewBufferString(data)
var name string
var age int
// Use fmt.Fscanf to read data from the buffer
fmt.Fscanf(buffer, "Name: %s Age: %d", &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 use fmt.Fscanf to read formatted data from a file.
Example
package main
import (
"fmt"
"os"
)
func main() {
// Open a file for reading
file, err := os.Open("product.txt")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
var item string
var price float64
// Use fmt.Fscanf to read data from the file
_, err = fmt.Fscanf(file, "Item: %s Price: %f", &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 product.txt:
Item: Laptop Price: 1299.99
Output:
Item: Laptop, Price: 1299.99
Real-World Use Case
Parsing Configuration Files
In real-world applications, fmt.Fscanf can be used to parse configuration files with a specific format.
Example
package main
import (
"bytes"
"fmt"
)
func main() {
// Simulate a configuration file with key-value pairs
configData := "host=localhost\nport=8080"
configFile := bytes.NewBufferString(configData)
var host string
var port int
// Use fmt.Fscanf to read configuration settings
fmt.Fscanf(configFile, "host=%s\nport=%d", &host, &port)
// Print the configuration settings
fmt.Printf("Host: %s\nPort: %d\n", host, port)
}
Output:
Host: localhost
Port: 8080
Conclusion
The fmt.Fscanf function is a powerful way to read and parse formatted input from any io.Reader using a format string. It is ideal for applications that need to process structured data from files, buffers, or network connections. By using fmt.Fscanf, you can efficiently extract and parse data from various sources in your Go programs.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment