Golang fmt Sscan Function

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

  1. Introduction
  2. Sscan Function Syntax
  3. Examples
    • Basic Usage
    • Reading Multiple Values
  4. Real-World Use Case
  5. 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.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare