Golang fmt Scan Function

The fmt.Scan function in Golang is part of the fmt package and is used to read input from the standard input (usually the console). It parses the input and stores the values in the specified variables. fmt.Scan reads space-separated values and stops reading at the first newline.

Table of Contents

  1. Introduction
  2. Scan Function Syntax
  3. Examples
    • Basic Usage
    • Reading Multiple Values
  4. Real-World Use Case
  5. Conclusion

Introduction

The fmt.Scan function allows you to capture user input directly from the console and store it in variables. It reads space-separated input and assigns the values to the provided arguments. This function is useful for simple user input handling in console applications.

Scan Function Syntax

The syntax for the fmt.Scan function is as follows:

func Scan(a ...interface{}) (n int, err error)

Parameters:

  • 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.Scan function to read a single value from user input.

Example

package main

import (
	"fmt"
)

func main() {
	var name string

	// Prompt the user for input
	fmt.Print("Enter your name: ")

	// Use fmt.Scan to read the input
	fmt.Scan(&name)

	// Print the captured input
	fmt.Println("Hello,", name)
}

Console Input/Output:

Enter your name: Alice
Hello, Alice

Reading Multiple Values

You can use fmt.Scan to read multiple space-separated values from user input.

Example

package main

import (
	"fmt"
)

func main() {
	var name string
	var age int

	// Prompt the user for input
	fmt.Print("Enter your name and age: ")

	// Use fmt.Scan to read multiple values
	fmt.Scan(&name, &age)

	// Print the captured input
	fmt.Printf("Name: %s, Age: %d\n", name, age)
}

Console Input/Output:

Enter your name and age: Bob 25
Name: Bob, Age: 25

Real-World Use Case

Basic User Interaction

In real-world applications, fmt.Scan can be used for simple user interactions, such as asking for confirmation or collecting small amounts of data.

Example

package main

import (
	"fmt"
)

func main() {
	var decision string

	// Prompt the user for a decision
	fmt.Print("Do you want to continue? (yes/no): ")

	// Use fmt.Scan to read the decision
	fmt.Scan(&decision)

	// Evaluate the user's input
	if decision == "yes" {
		fmt.Println("Continuing...")
	} else if decision == "no" {
		fmt.Println("Exiting...")
	} else {
		fmt.Println("Invalid input. Please enter 'yes' or 'no'.")
	}
}

Console Input/Output:

Do you want to continue? (yes/no): yes
Continuing...

Conclusion

The fmt.Scan function is a simple way to read and parse user input from the console in Go. It allows you to capture space-separated values and store them in variables, making it ideal for basic user interactions in console applications. By using fmt.Scan, you can efficiently handle user input 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