🎓 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.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
SscanlnFunction 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.
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