🎓 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.Sscan function in Golang is part of the fmt package and is used to scan and parse input from a string. It reads space-separated values from the provided string and assigns them to the specified variables. This function is useful for extracting values from strings that contain structured data.
Table of Contents
- Introduction
SscanFunction Syntax- Examples
- Basic Usage
- Reading Multiple Values
- Real-World Use Case
- Conclusion
Introduction
The fmt.Sscan function allows you to parse values from a string based on their space-separated representation. It is similar to fmt.Scan, but instead of reading from standard input, it reads from a string. This function is helpful when you need to extract data from strings, such as parsing configuration values or processing input from a file or network.
Sscan Function Syntax
The syntax for the fmt.Sscan function is as follows:
func Sscan(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.Sscan function to extract a single value from a string.
Example
package main
import (
"fmt"
)
func main() {
var name string
// Define a string containing the data
data := "Alice"
// Use fmt.Sscan to parse the data from the string
_, err := fmt.Sscan(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.Sscan to extract multiple space-separated values from a string.
Example
package main
import (
"fmt"
)
func main() {
var name string
var age int
// Define a string containing the data
data := "Bob 25"
// Use fmt.Sscan to parse the data from the string
_, err := fmt.Sscan(data, &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: Bob, Age: 25
Real-World Use Case
Parsing Configuration Data
In real-world applications, fmt.Sscan can be used to parse 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 := "localhost 8080"
// Use fmt.Sscan to extract configuration values
_, err := fmt.Sscan(config, &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.Sscan function is a convenient way to parse space-separated values from a string in Go. It allows you to extract data from structured strings and assign them to variables, making it ideal for processing configuration data, command-line arguments, or other structured input. By using fmt.Sscan, you can efficiently parse and handle string data 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