The fmt.Sscanf
function in Golang is part of the fmt
package and is used to scan and parse formatted input from a string according to a specified format. It reads data from a string and assigns the parsed values to the specified variables. This function is useful for extracting values from strings that contain structured data in a specific format.
Table of Contents
- Introduction
Sscanf
Function Syntax- Examples
- Basic Usage
- Reading Multiple Values with Format Specifiers
- Real-World Use Case
- Conclusion
Introduction
The fmt.Sscanf
function allows you to parse values from a string using format specifiers. This provides more control over how input is processed compared to fmt.Sscan
, which reads space-separated values. fmt.Sscanf
is particularly useful when you need to extract data from strings with a specific structure or delimiter.
Sscanf Function Syntax
The syntax for the fmt.Sscanf
function is as follows:
func Sscanf(str string, format string, a ...interface{}) (n int, err error)
Parameters:
str
: The input string to be scanned.format
: A format string containing format specifiers that define how to parse the input.a
: Pointers to variables where the scanned data will be stored. Each variable should match the expected input type defined by the format specifiers.
Returns:
n
: The number of items successfully scanned and assigned.err
: An error if one occurred during scanning.
Common Format Specifiers:
%s
: String%d
: Integer (base 10)%f
: Floating-point number%t
: Boolean%v
: Default format for the type
Examples
Basic Usage
This example demonstrates how to use the fmt.Sscanf
function to extract formatted input from a string.
Example
package main
import (
"fmt"
)
func main() {
var name string
var age int
// Define a string containing the data
data := "Name: Alice Age: 30"
// Use fmt.Sscanf to parse the data from the string
_, err := fmt.Sscanf(data, "Name: %s Age: %d", &name, &age)
if err != nil {
fmt.Println("Error scanning:", err)
return
}
// Print the extracted values
fmt.Printf("Name: %s, Age: %d\n", name, age)
}
Output:
Name: Alice, Age: 30
Reading Multiple Values with Format Specifiers
You can use fmt.Sscanf
to extract multiple values from a string using specific format specifiers.
Example
package main
import (
"fmt"
)
func main() {
var day, month, year int
// Define a string containing the date
dateStr := "15/08/2024"
// Use fmt.Sscanf to parse the date from the string
_, err := fmt.Sscanf(dateStr, "%d/%d/%d", &day, &month, &year)
if err != nil {
fmt.Println("Error scanning date:", err)
return
}
// Print the extracted date
fmt.Printf("Day: %02d, Month: %02d, Year: %d\n", day, month, year)
}
Output:
Day: 15, Month: 08, Year: 2024
Real-World Use Case
Parsing Configuration Files
In real-world applications, fmt.Sscanf
can be used to parse structured configuration data from a string or file.
Example
package main
import (
"fmt"
)
func main() {
var host string
var port int
// Simulate a configuration string
config := "host=localhost port=8080"
// Use fmt.Sscanf to extract configuration values
_, err := fmt.Sscanf(config, "host=%s port=%d", &host, &port)
if err != nil {
fmt.Println("Error scanning configuration:", err)
return
}
// Print the configuration values
fmt.Printf("Host: %s, Port: %d\n", host, port)
}
Output:
Host: localhost, Port: 8080
Conclusion
The fmt.Sscanf
function is a powerful way to parse formatted input from a string in Go. By using format specifiers, you can control how data is extracted and assigned to variables, making it ideal for applications that require structured input handling. Whether you're processing configuration data, parsing dates, or handling other structured input, fmt.Sscanf
provides the flexibility needed to interpret string data in your Go programs.
Comments
Post a Comment
Leave Comment