The fmt.Sscanln
function in Golang is part of the fmt
package and is used to scan and parse input from a string until a newline character is encountered. It reads space-separated values from the string and assigns them to the specified variables. This function is useful when you want to extract values from a string that contains multiple lines of data or when processing line-based input.
Table of Contents
- Introduction
Sscanln
Function Syntax- Examples
- Basic Usage
- Reading Multiple Values
- Real-World Use Case
- Conclusion
Introduction
The fmt.Sscanln
function allows you to parse space-separated values from a string, stopping at a newline character. It is similar to fmt.Sscan
, but fmt.Sscanln
stops scanning as soon as it reaches the end of a line. This function is particularly useful for processing input where each line contains separate pieces of data, such as reading log entries or parsing user input from a file.
Sscanln Function Syntax
The syntax for the fmt.Sscanln
function is as follows:
func Sscanln(str string, a ...interface{}) (n int, err error)
Parameters:
str
: The input string to be scanned.a
: Pointers to variables where the scanned data will be stored. Each variable should correspond to the expected input type.
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.Sscanln
function to extract a single line of input from a string.
Example
package main
import (
"fmt"
)
func main() {
var name string
// Define a string containing the data
data := "Alice\nBob\nCharlie"
// Use fmt.Sscanln to parse the first line from the string
_, err := fmt.Sscanln(data, &name)
if err != nil {
fmt.Println("Error scanning:", err)
return
}
// Print the extracted value
fmt.Println("Name:", name)
}
Output:
Name: Alice
Reading Multiple Values
You can use fmt.Sscanln
to extract multiple space-separated values from a single line of input.
Example
package main
import (
"fmt"
)
func main() {
var product string
var price float64
// Define a string containing the data
data := "Laptop 999.99\nSmartphone 799.99\nTablet 499.99"
// Use fmt.Sscanln to parse the first line from the string
_, err := fmt.Sscanln(data, &product, &price)
if err != nil {
fmt.Println("Error scanning:", err)
return
}
// Print the extracted values
fmt.Printf("Product: %s, Price: $%.2f\n", product, price)
}
Output:
Product: Laptop, Price: $999.99
Real-World Use Case
Parsing Log Entries
In real-world applications, fmt.Sscanln
can be used to parse log entries from a string or file, processing each line separately.
Example
package main
import (
"fmt"
)
func main() {
var timestamp, level, message string
// Simulate a log entry string
logEntry := "2024-08-06 INFO Application started\n2024-08-06 WARN Low disk space"
// Use fmt.Sscanln to extract the first log entry
_, err := fmt.Sscanln(logEntry, ×tamp, &level, &message)
if err != nil {
fmt.Println("Error scanning log entry:", err)
return
}
// Print the log entry details
fmt.Printf("Timestamp: %s, Level: %s, Message: %s\n", timestamp, level, message)
}
Output:
Timestamp: 2024-08-06, Level: INFO, Message: Application started
Conclusion
The fmt.Sscanln
function is a straightforward way to parse space-separated values from a string in Go. It stops scanning at a newline, making it ideal for applications that process line-based input. By using fmt.Sscanln
, you can efficiently extract data from strings containing multiple lines, such as log files or user input, in your Go programs.
Comments
Post a Comment
Leave Comment